The JavaScript Math object includes a method that provides pseudo-random numbers. Unlike many other programming languages, JavaScript does not allow you to seed the number generator with your own source of randomness. Different implementations differ in how close to truly random the numbers provided are, but in general, they should be good enough to use for fun or artistic uses but if you need random numbers for legally regulated or security purposes, you should find a better source of randomness. The

*math.random()*method returns a random number that is either 0 or between 0 and 1. Some versions of the Opera browser have a non-standards compliant

*math.random()*method that will very rarely return 1. You need to somehow handle this unexpected result – the easiest suggestion I've seen is to use the modulo operator with one against the value returned by

*math.random()*, this will return the original value if the random number is in the defined range and 0 if you got a 1. Also, I find that I don't need a random decimal less than one, but rather a random integer. To get a random integer, you multiply the value from

*math.random()*by the number of potential random numbers you want (X) and finding the previous or next integer. The math object provides floor and ceiling methods that you can use for this purpose. If you use the the

*math.floor()*method, you will get a number from 0 to (X-1). If you use the

*math.ceiling()*method you will get an integer from 1 to X.

To find a random number between 0 and (X-1), use:

*Math.floor((Math.random() % 1) * X)*

These are particularly useful for finding a random array element.

To find a random number between 1 and X, use:

*Math.ceiling((Math.random() % 1) * X)*

Tasks like this that are fairly common but require quite a bit of typing are good candidates for being turned into a function.

For a discussion of the difference between truly random numbers and the pseudo-random numbers we are using here, a source of truly random numbers, and some examples of fun things you can do with random or pseudo-random numbers check out www.random.org