JavaScript Math.random()
Definition and Usage
The Math.random() method returns a random number from 0 (inclusive) up to but not including 1 (exclusive).
Example 2
Return a random number between 1 and 10:
Math.floor((Math.random() * 10) + 1);
Try it Yourself »
Example 3
Return a random number between 1 and 100:
Math.floor((Math.random() * 100) + 1);
Try it Yourself »
Browser Support
Math.random()
is an ECMAScript1 (ES1) feature.
ES1 (JavaScript 1997) is fully supported in all browsers:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |
Syntax
Math.random()
Parameters
None |
Return Value
A number representing a number from 0 up to but not including 1. |