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
Editor's Picks Articles
Top Ten Articles
Content copyright © 2022 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.