JS Random Number Function


Math.random() is a function of Math Object for generate a random number between 0 and 1.


Math.random();



Generate a number between 1 and 100:

var seed = Math.random();
var n = seed * 100 + 1;
n = Math.floor(n);
alert(n);



Generate a number between 10 and 100:

var seed = Math.random();
var n = seed * 90 + 10;
n = Math.floor(n);
alert(n);



Generate a number between 23 and 42:

var seed = Math.random();
var n = seed * (42-23) + 23;
n = Math.floor(n);
alert(n);



endmemo.com © 2020  Terms of Use | Home