Generating Random Numbers
Problem
You need to generate a random number in a certain range.
Solution
Use JavaScript’s Math.random() to get floating-point numbers from 0 <= x < 1.0. Use multiplication and Math.floor to get a number in a certain range.
Discussion
This is a straight lift from JavaScript.
Note that JavaScript’s Math.random() does not allow you to seed the random number generator to force certain values. See Generating Predictable Random Numbers for that.
To generate a number from 0 up to (but not including) n, multiply by n. To generate a number from 1 to n (inclusive), multiply by n and add 1.