CSC 107      Fall 2003

Appendix E: random.js Library


// File: random.js // Author: Dave Reed // // This file contains several routines for generating random values. // Source code is available at http://www.prenhall.com/reed/random.js ////////////////////////////////////////////////////////////////////// function RandomNum(low, high) // Given : low <= high // Returns : a random number in the range [low, high) { return Math.random()*(high-low) + low; } function RandomInt(low, high) // Given : low <= high // Returns : a random integer in the range [low, high] { return Math.floor(Math.random()*(high-low+1)) + low; } function RandomChar(str) // Given : str is a nonempty string // Returns: a random character from the string { return str.charAt(RandomInt(0, str.length-1)); } function RandomOneOf(list) // Given : list is a nonempty list (array) // Returns: a random item from the list { return list[RandomInt(0, list.length-1)]; }