JavaScript Math.random()
Examples
let x = Math.random();
Try it Yourself »
Return a random number between 0 (inclusive) and 10 (exclusive):
let x = Math.random() * 10;
Try it Yourself »
Return a random number between 0 (inclusive) and 100 (exclusive):
let x = Math.random() * 100;
Try it Yourself »
A random whole number between 1 and 10 (inclusive):
let x = Math.floor((Math.random() * 10) + 1);
Try it Yourself »
A random whole number between 1 and 100 (inclusive):
let x = Math.floor((Math.random() * 100) + 1);
Try it Yourself »
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
Math.random()
Parameters
NONE |
Return Value
Type | Description |
Number | A random number from 0 (inclusive) up to but not including 1 (exclusive). |
Browser Support
Math.random()
is an ECMAScript1 (JavaScript 1997) feature.
It is supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |