How to get one digit random number in javascript?

Gowsikan picture Gowsikan · Jan 2, 2013 · Viewed 20.6k times · Source

Possible Duplicate:
Generating random numbers in Javascript in a specific range?

Can some one tell me how to get one digit random number(1,2,3,.. not 0.1,0.2,.. or 1.0,5.0,..) using Math.random() or some other way in javascript?

Answer

Blender picture Blender · Jan 2, 2013

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)