Math.random()
Returns a pseudo-random floating point number in the range [0, 1).
Syntax
Math.random() Returns
number — A number greater than or equal to 0 and less than 1.
Examples
const n = Math.random();
console.log(n >= 0 && n < 1);
Output
true
const roll = Math.floor(Math.random() * 6) + 1;
console.log(roll >= 1 && roll <= 6);
Output
true
Notes
Never returns exactly 1. NOT cryptographically secure - use
`crypto.getRandomValues()` for security-sensitive randomness. For an integer
in [min, max] use `Math.floor(Math.random() * (max - min + 1)) + min`.
Browser & runtime support
| Environment | Since version |
|---|---|
| chrome | 1.0 |
| firefox | 1.0 |
| safari | 1.0 |
| edge | 12 |
| node | 0.10 |