Possible Duplicate:
Generating random whole numbers in JavaScript in a specific range
How can I get one-digit random numbers (1, 2, 3, ..., not 0.1, 0.2, ... or 1.0, 5.0, ...) using Math.random() or some other way in JavaScript?
Math.random()
returns a float between 0
and 1
, so just multiply it by 10
and turn it into an integer:
Math.floor(Math.random() * 10)
Or something a little shorter:
~~(Math.random() * 10)