Random Numbers
Question: How do I generate random numbers in JavaScript?
Answer: To generate random floating-point numbers in the range from 0 to 1,
use the Math.random()
method:
num = Math.random() // num is random, from 0 to 1
If you need random floating-point numbers in the range from
A
to
B
,
use this code:
num = A + (B-A)*Math.random() // num is random, from A to B
JavaScripter.net.
Copyright
© 1999-2006, Alexei Kourbatov