Math.random()
used with Math.floor()
can be used to return random integers.
There is no such thing as JavaScript integers.
We are talking about numbers with no decimals here.
Example
// Returns a random integer from 0 to 9:
Math.floor(Math.random() * 10);
Math.floor(Math.random() * 10);
Example
// Returns a random integer from 0 to 10:
Math.floor(Math.random() * 11);
Math.floor(Math.random() * 11);
Example
// Returns a random integer from 0 to 99:
Math.floor(Math.random() * 100);
Math.floor(Math.random() * 100);
Example
// Returns a random integer from 0 to 100:
Math.floor(Math.random() * 101);
Math.floor(Math.random() * 101);
Example
// Returns a random integer from 1 to 10:
Math.floor(Math.random() * 10) + 1;
Math.floor(Math.random() * 10) + 1;
Example
// Returns a random integer from 1 to 100:
Math.floor(Math.random() * 100) + 1;
Math.floor(Math.random() * 100) + 1;
Practice Excercise Practice now