Back # Math.random

The ** Math.random()** function returns a floating-point, pseudo-random number in the range

`[0, 1)`

that is, from 0 (inclusive) up to but not including 1 (exclusive), 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 the method.

`Math.random()`

`Math.random()`

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 for `Math.random()`

itself) aren't exact. If extremely large bounds are chosen (2^{53} or higher), it's possible in *extremely* rare cases to calculate the usually-excluded upper bound.

// Returns a random number between 0 (inclusive) and 1 (exclusive) function getRandom() { return Math.random(); }

// Returns a random number between min (inclusive) and max (exclusive) function getRandomArbitrary(min, max) { return Math.random() * (max - min) + min; }

// Returns a random integer between min (included) and max (excluded) // Using Math.round() will give you a non-uniform distribution! function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min)) + min; }

// Returns a random integer between min (included) and max (included) // Using Math.round() will give you a non-uniform distribution! function getRandomIntInclusive(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; }

Created by Mozilla Contributors, license: CC-BY-SA 2.5