The Math.random()
function returns a floating-point, pseudo-random number in the range from 0 inclusive up to but not including 1 — 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.
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 window.crypto.getRandomValues()
method.
Math.random()
A floating-point, pseudo-random number between 0
(inclusive) and 1 (exclusive).
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 (253 or higher), it's possible in extremely rare cases to calculate the usually-excluded upper bound.
function getRandom() { return Math.random(); }
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; }
This example returns a random integer between the specified values. The value is no lower than min
(or the next integer greater than min
if min
isn't an integer), and is less than (but not equal to) max
.
function getRandomInt(min, max) { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min)) + min; //The maximum is exclusive and the minimum is inclusive }
It might be tempting to use rounding 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.
While the getRandomInt()
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? The getRandomIntInclusive()
function below accomplishes that.
function getRandomIntInclusive(min, max) { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min + 1)) + min; //The maximum is inclusive and the minimum is inclusive }
Specification | Status | Comment |
---|---|---|
ECMAScript 1st Edition (ECMA-262) | Standard | Initial definition. JavaScript 1.0 (UNIX Only) / JavaScript 1.1 (All platforms). |
ECMAScript 5.1 (ECMA-262) The definition of 'Math.random' in that specification. | Standard | |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Math.random' in that specification. | Standard | |
ECMAScript Latest Draft (ECMA-262) The definition of 'Math.random' in that specification. | Draft |
Feature | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | Yes | Yes | Yes | Yes | Yes | Yes |
Feature | Android webview | Chrome for Android | Edge mobile | Firefox for Android | Opera Android | iOS Safari | Samsung Internet |
---|---|---|---|---|---|---|---|
Basic support | Yes | Yes | Yes | Yes | Yes | Yes | ? |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random