Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the minimal non-zero value Math.random() can return in javascript

I know computers can't work with continuums. Math.random() javascript function returns a floating-point number between 0 (inclusively) and 1 (exclusively). I wonder what is the minimal non-zero number it can return. What "step" has this function?

like image 523
Maksim Medvedev Avatar asked Feb 27 '15 16:02

Maksim Medvedev


People also ask

What does Math random return in JavaScript?

The Math.random() function 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.

Can Math random return 1 JS?

The Math.random() method returns a random number from 0 (inclusive) up to but not including 1 (exclusive).

Is Math random () really random?

How random is Math. random()? It may be pointed out that the number returned by Math. random() is a pseudo-random number as no computer can generate a truly random number, that exhibits randomness over all scales and over all sizes of data sets.

Is there a random function in JavaScript?

In JavaScript, random() is a function that is used to return a pseudo-random number or random number within a range. Because the random() function is a static function of the Math object, it must be invoked through the placeholder object called Math.


1 Answers

The standard surely doesn't express this value, so it depends on the implementation (and exaggerating a bit on this point, probably even an implementation that aways returns 0.42 as result for Math.random() is still compliant with the specification).

The smallest positive number that can be represented by a 64-bit normalized floating point number in IEEE754 format is 2−1022, i.e. 2.2250738585072014 × 10−308.

However the floating point representation uses a varying resolution, depending on the magnitude.

For numbers close to 1 the resolution is 2-53. Probably (just probably) many implementations pick a random integer number n between 0 and 253-1 and use as result n/9007199254740992.

like image 125
6502 Avatar answered Oct 16 '22 04:10

6502