precision of Math.random()
Asked Answered
H

4

14

What is the precision of JavaScript's Math.random() function?

Holub answered 27/7, 2010 at 14:5 Comment(1)
Right, that's what I meant. Thanks!Holub
M
11

Math.random() generates a floating point number of 16 decimal places greater than or equal to zero and less than 1.

Monody answered 27/7, 2010 at 14:7 Comment(4)
Javascript floating point numbers are 64-bit IEEE 754 values, so it's not exactly 16 decimal places.Waxbill
@volatile: If your precision is not a power of 10.Horne
@Waxbill This is true, I was imprecise in my answer. bump.Monody
Do you have any references for this precision?Akee
T
10

It's browser/JavaScript engine dependent.

The maximum possible precision is 52 bits, because Math.random returns a double-precision floating-point between 0 (inclusive) and 1 (exclusive). This maximum corresponds to roughly 16 decimals, see Sly1024's answer.

In practice, many browsers give a smaller precision. This seems to be the current state:

Run the following piece of code several times and you'll see that the trailing 21 bits are consistently 0 in Chrome and Safari.

console.log((Math.random() * Math.pow(2,53)).toString(2));

Tong answered 14/1, 2015 at 13:45 Comment(5)
IE11: 52 random bits, although the code above gives results like 111000000101010100010110011011110111000111010101110.1 sometimes.Eidolon
Chrome seems to have upgraded their precision, I can no longer observe the 21 consistently 0 bits.Cynara
Chrome (and in extension, all Blink-based browsers and V8-based products, such as Electron-apps and Node.js) updated the Math.random() precision to 128 bits in 2015.Maquette
@Xyz, may I know the related post or source code change about the change of precision from 32 bits to 128 bits in 2015?Tollgate
@sken130, sure! Patch and bug issue. Not sure which versions of Chrome and Safari, this shipped in. Clearification: The underlying precision changed to 64 bits in 2015. As mentioned, only 52 bits of a 64 bit IEEE 754 floating point number (as javascript uses) are actual precision. The rest is the sign bit and the exponent. Still, the underlying precisions was updated to 64 bits. I think I got the 128 bits mixed up.Maquette
E
5

According to wikipedia : Double-precision floating-point format the fraction part is 52 bits, and we know that the number will be between 0 and 1 (I think not including 1), so the exponent is -1, that leaves us with 52 random bits.

The 52 random bits give you 52 bit precision, which in base 10 is about

52*log10(2) ~= 15.653559774527022151114422525674 digits.
Etui answered 15/11, 2012 at 16:52 Comment(0)
M
1

Math.random() in Javascript returns a floating point number in the range 0..1, in the native javascript format, which is a 64 bit IEEE 754 double-precision floating point format. Due to the fact that this format uses 52 bits for the actual precision, you are effectively limited to 52 bits of precision in modern Javascript implementations.

In real usage (in base 10), this normally roughly translates into 15-16 digits. But, it can also be fewer decimal places if the number ends in zeroes.

If you are using a really ancient Javascript implementation (Internet Explorer, Safari from 2016 or Chrome from 2015 or some older out-of-life versions of Node), Math.random() may use an algorithm with only on a 32 bit precision, which means the last 20 bits of precision will just be 0, meaning you will get significantly less precision.

If you want more precision, you can use Crypto.getRandomValues(new BigUint64Array(1)) which will give you 64 bits of true precision using a verified CSPRNG algorithm.

Maquette answered 23/1, 2023 at 12:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.