JavaScript Math.random()
Examples
Return a random number between 0 (inclusive) and 10 (exclusive):
Return a random number between 0 (inclusive) and 100 (exclusive):
A random whole number between 1 and 10 (inclusive):
A random whole number between 1 and 100 (inclusive):
Description
The Math.random() method returns a random floating point number between 0 (inclusive)
and 1 (exclusive).
Note
Math.random() does not return a cryptographically secure number.
If you need a cryptographically secure number, use this Crypto API method:
crypto.getRandomValues()
Syntax
Parameters
Return Value
Browser Support
Math.random() is an ECMAScript1 (JavaScript 1997) feature.
It is supported in all browsers:
| Chrome | Edge | Firefox | Safari | Opera |