Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JavaScript · Function

Math.random

Math.random() returns an approximately uniformly distributed pseudo-random floating-point number from 0 inclusive to 1 exclusive. It accepts no arguments and is not suitable for cryptographic security.

Since
July 2015

How it works

Calling Math.random() produces a pseudo-random floating-point value in the interval from 0, inclusive, to 1, exclusive. Multiply it by a range width to scale the interval, then use Math.floor() when an integer is required.

Syntax

Math.random()

Returns

A floating-point, pseudo-random number between 0 (inclusive) and 1 (exclusive).

Key points

  • The result is a floating-point pseudo-random number in the half-open interval [0, 1).
  • Values are approximately uniformly distributed across the supported interval.
  • Multiplying by a range width and adding a lower bound transforms the result into a custom range.
  • The runtime chooses the generator's initial seed rather than exposing seed control to JavaScript.

Common mistakes

  • Do not use this function for passwords, tokens, keys, or other security-sensitive values; use the Web Crypto API instead.
  • The seed is implementation-selected, so callers cannot choose or reset it for reproducible sequences.

Example: Generate a random integer

function randomInt(max) {
  return Math.floor(Math.random() * max);
}

console.log(randomInt(3)); // 0, 1, or 2

Scales the returned value to a range and floors it to produce an integer from 0 through max - 1.

When to use it

Non-security random integers

Choose an array index or generate a bounded integer for simulations and games.

Scaled floating-point ranges

Create a fractional value within an application-defined numeric interval.

Approximate randomized behavior

Vary visual effects, test data, or randomized UI behavior where unpredictability is not a security requirement.

Interview questions

QWhat does Math.random() return?

It returns a pseudo-random floating-point number greater than or equal to 0 and less than 1. The distribution is approximately uniform over that interval.

Follow-up: How would you produce an integer from 0 through 9? Multiply by 10 and apply Math.floor(), yielding Math.floor(Math.random() * 10).

QHow do you scale Math.random() to a range?

Use Math.random() * (max - min) + min for a floating-point value in the interval [min, max). The lower bound may be returned, while the upper bound is excluded.

Follow-up: How would you include an integer upper bound? Use Math.floor(Math.random() * (max - min + 1)) + min for integer bounds.

QWhat are Math.random()'s security and seeding limitations?

No arguments are accepted. The function's pseudo-random output is not cryptographically secure, so security-sensitive randomness should come from Crypto.getRandomValues().

Follow-up: Why not seed Math.random() yourself? Its initial seed is selected by the implementation and cannot be chosen or reset by the user.

  • Math.floor()
  • Crypto.getRandomValues()
  • Math

Source: Math.random() - JavaScript | MDN. Checked 2026-10-15.