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

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

Math.random()

Return value

A number in [0, 1).

Examples

Always in [0, 1)Try it yourself
const n = Math.random();
console.log(n >= 0 && n < 1);

Output

true
Random integers in a rangeTry it yourself
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 use crypto.getRandomValues() or crypto.randomUUID().
  • Math.round(Math.random() * 6) makes 0 and 6 half as likely as the other numbers; use the floor formula shown above.

Version history

VersionChange
JavaScript ES1Added

Was this page helpful?