To generate a random number between 0 and X, use the below javascript:
var myRandomNumber = Math.floor(Math.random()*(X+1))
Therefore, if you want from 0 and 100, you simply need:
var myRandomNumber = Math.floor(Math.random()*101)
Now, a bit more complicated! If we want a number between the range of X and Y, follow the below formula:
var myRandomNumber = Math.floor(Math.random()*(Y-X))+X
And another real world example. Assume we want a random number from 79 and 473:
var myRandomNumber = Math.floor(Math.random()*394)+79





Leave a Reply