JavaScript random() method
- Previous Page pow()
- Next Page round()
- Go Up One Level JavaScript Math Reference Manual
Definition and usage
random()
The method returns a random number from 0 (inclusive) to 1 (exclusive).
Example
Example 1
Returns a random number between 0 (inclusive) and 1 (exclusive):
Math.random();
Example 2
Returns a random number between 1 and 10:
Math.floor((Math.random() * 10) + 1);
Example 3
Returns a random number between 1 and 100:
Math.floor((Math.random() * 100) + 1);
Syntax
Math.random()
Parameter
No parameters.
Technical details
Return value: | A pseudo-random number between 0.0 and 1.0. |
---|---|
JavaScript Version: | ECMAScript 1 |
Browser Support
Method | Chrome | IE | Firefox | Safari | Opera |
---|---|---|---|---|---|
random() | Support | Support | Support | Support | Support |
Related Pages
Tutorial:JavaScript Math
- Previous Page pow()
- Next Page round()
- Go Up One Level JavaScript Math Reference Manual