JavaScript (ES2026) reference · Math Methods and Constants
JavaScript Math.random() Method
Return a pseudo-random number from 0 (inclusive) up to 1 (exclusive). Not suitable for security; use crypto for that.
JavaScript (ES2026) Since ES1
Syntax
Math.random()
Return value
A number in [0, 1).
Examples
const n = Math.random();
console.log(n >= 0 && n < 1);
Output
true
const randomInt = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
const rolls = Array.from({ length: 1000 }, () => randomInt(1, 6));
console.log(Math.min(...rolls), Math.max(...rolls));
Output
1 6
Common mistakes
Math.random()is not cryptographically secure. For tokens, IDs or passwords usecrypto.getRandomValues()orcrypto.randomUUID().Math.round(Math.random() * 6)makes 0 and 6 half as likely as the other numbers; use thefloorformula shown above.
Version history
| Version | Change |
|---|---|
| JavaScript ES1 | Added |
Related
Math.floor()· Round a number down to the integer below it: Math.floor(4.9) is 4 and Math.floor(-4.1) is -5.- JavaScript Math · tutorial
- JavaScript Numbers · tutorial