JavaScript Random
JavaScript Random
w3schools.com LOG IN
JavaScript Random
❮ Previous Next ❯
Math.random()
Math.random() returns a random number between 0 (inclusive), and 1 (exclusive):
Example
Math.random(); // returns a random number
Try it Yourself »
https://www.w3schools.com/js/js_random.asp 1/8
1/16/2021 JavaScript Random
Example
Try it Yourself »
Example
Try it Yourself »
Example
Try it Yourself »
Example
Try it Yourself »
Example
https://www.w3schools.com/js/js_random.asp 2/8
1/16/2021 JavaScript Random
Try it Yourself »
Example
Math.floor(Math.random() * 100) + 1; // returns a random integer from 1 to 100
Try it Yourself »
This JavaScript function always returns a random number between min (included) and
max (excluded):
Example
function getRndInteger(min, max) {
return Math.floor(Math.random() * (max - min) ) + min;
}
https://www.w3schools.com/js/js_random.asp 3/8
1/16/2021 JavaScript Random
Try it Yourself »
This JavaScript function always returns a random number between min and max (both
included):
Example
Try it Yourself »
❮ Previous Next ❯
https://www.w3schools.com/js/js_random.asp 4/8