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 1If you need random floating-point numbers in the range from
A to B (A<B),
use this code:
num = A + (B-A)*Math.random() // num is random, from A to BFor a random integer in the range from
M to N
(where M and N are two integers,
M < Nnum = Math.floor(M + (1+N-M)*Math.random()) // num is random integer from M to N
Copyright © 1999-2012, JavaScripter.net.