Random Numbers in JavaScript

Random Numbers in JavaScript
Random numbers have all sorts of uses, from the fun to the practical. Most games involve some sort of randomness in the form of things like dice or shuffled cards. If you want to display a random quote when someone visits your page, you need a source of randomness. Random numbers aren't all fun and games, random numbers are used in cryptography, for random sampling, for modeling, and for putting things in random order (such as candidates on a ballot.) Some of these uses don't use numbers directly, but you can convert from random numbers to random things by putting the things you want to choose in rows and randomly picking a row. In programming, arrays work well for this. Random numbers aren't always truly random. If you roll a set of fair dice, you will get random numbers, however the methods that computers use to pick random numbers are often pseudo-random numbers, meaning that they appear to be random on the surface but they are actually either predictable or a repeating series. For some things, this isn't an important difference. If you are displaying a random quote or joke each time someone loads your web page, it probably doesn't matter someone can predict when a specific quote will show up and you can safely use pseudo-random numbers (for that matter, you could probably just show your quotes in the order you happened to write them down.) If you are trying to do random drug testing of Tour de France athletes, it is crucial that athletes not be able to predict when or whether they will be tested.

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

This site needs an editor - click to learn more!

You Should Also Read:
Java Script Articles
JavaScript and Java Site and Newsletter
Beginners Resources

Related Articles
Editor's Picks Articles
Top Ten Articles
Previous Features
Site Map

Content copyright © 2023 by Julie L Baumler. All rights reserved.
This content was written by Julie L Baumler. If you wish to use this content in any manner, you need written permission. Contact BellaOnline Administration for details.