Math.random()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
TheMath.random() static method returns a floating-point, pseudo-random number that's greater than or equal to 0 and less than 1, with approximately uniform distribution over that range — which you can then scale to your desired range. The implementation selects the initial seed to the random number generation algorithm; it cannot be chosen or reset by the user.
Note:Math.random()does not provide cryptographically secure random numbers. Do not use them for anything related to security. Use the Web Crypto API instead, and more precisely theCrypto.getRandomValues() method.
In this article
Try it
function getRandomInt(max) { return Math.floor(Math.random() * max);}console.log(getRandomInt(3));// Expected output: 0, 1 or 2console.log(getRandomInt(1));// Expected output: 0console.log(Math.random());// Expected output: a number from 0 to <1Syntax
Math.random()Parameters
None.
Return value
A floating-point, pseudo-random number between 0 (inclusive) and 1 (exclusive).
Examples
Note that as numbers in JavaScript are IEEE 754 floating point numbers with round-to-nearest-even behavior, the ranges claimed for the functions below (excluding the one forMath.random() itself) aren't exact. Usually, the claimed upper bound is not attainable, but ifMath.random() returns a number very close to 1, the tiny difference may not be representable at the requested maximum, therefore causing the upper bound to be attained.
Getting a random number between 0 (inclusive) and 1 (exclusive)
function getRandom() { return Math.random();}Getting a random number between two values
This example returns a random number between the specified values. The returned value is no lower than (and may possibly equal)min, and is less than (and not equal)max.
function getRandomArbitrary(min, max) { return Math.random() * (max - min) + min;}Getting a random integer between two values
This example returns a randominteger between the specified values. The value is no lower thanmin (or the next integer greater thanmin ifmin isn't an integer), and is less than (but not equal to)max.
function getRandomInt(min, max) { const minCeiled = Math.ceil(min); const maxFloored = Math.floor(max); return Math.floor(Math.random() * (maxFloored - minCeiled) + minCeiled); // The maximum is exclusive and the minimum is inclusive}Note:It might be tempting to useMath.round() to accomplish that, but doing so would cause your random numbers to follow a non-uniform distribution, which may not be acceptable for your needs.
Getting a random integer between two values, inclusive
While thegetRandomInt() function above is inclusive at the minimum, it's exclusive at the maximum. What if you need the results to be inclusive at both the minimum and the maximum? ThegetRandomIntInclusive() function below accomplishes that.
function getRandomIntInclusive(min, max) { const minCeiled = Math.ceil(min); const maxFloored = Math.floor(max); return Math.floor(Math.random() * (maxFloored - minCeiled + 1) + minCeiled); // The maximum is inclusive and the minimum is inclusive}Specifications
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-math.random> |