Odds to get the usually excluded upper-bound with Math.random()
<p>This may look more like a math question but as it is exclusively linked to Javascript's pseudo-random number generator I guess it is a good fit for SO. If not, feel free to move it elsewhere.</p>

<p>First off, I'm aware that ES does not specify the algorithm to be used in the pseudo-random number generator - <code>Math.random()</code> -, but it does specify that the range should have an approximate uniform distribution:</p>

<blockquote>
  <h2><a href="http://www.ecma-international.org/ecma-262/5.1/#sec-15.8.2.14" rel="nofollow">15.8.2.14 random ( )</a></h2>
  
  <p>Returns a Number value with positive sign, greater than or equal to 0 but less than 1, chosen randomly or pseudo randomly with <strong>approximately uniform distribution over that range</strong>, using an implementation-dependent algorithm or strategy. This function takes no arguments.</p>
</blockquote>

<p>So far, so good. Now I've recently stumbled upon this piece of data from <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Math/random" rel="nofollow">MDN</a>:</p>

<blockquote>
  <p>Note that as numbers in JavaScript are IEEE 754 floating point numbers with round-to-nearest-even behavior, these ranges, excluding the one for <code>Math.random()</code> itself, aren't exact, and depending on the bounds it's possible in extremely rare cases (on the order of <strong>1 in 2^62</strong>) to calculate the usually-excluded upper bound.</p>
</blockquote>

<p>Okay. It led me to some testing, the results are (obviously) the same on Chrome console and Firefox's Firebug:</p>

<pre><code>&gt;&gt; 0.99999999999999995
1
&gt;&gt; 0.999999999999999945
1
&gt;&gt; 0.999999999999999944
0.9999999999999999
</code></pre>

<p>Let's put it in a simple practical example to make my question more clear:</p>

<pre><code>Math.floor(Math.random() * 1)
</code></pre>

<p>Considering the code above, IEEE 757 floating point numbers with round-to-nearest-even behavior, under the assessment of <code>Math.random()</code> range being evenly distributed, I concluded that the odds for it to return the usually excluded upper bound (<code>1</code> in my code above) would be <code>0.000000000000000055555...</code>, that is approximately <code>1/18,000,000,000,000,000</code>.</p>

<p>Looking at the MDN number now, <code>1/2^62</code> evaluates to <code>1/4,611,686,018,427,387,904</code>, that is, over 200 times smaller than the result from my calc.</p>

<p>Am I doing the wrong math? Is Firefox's pseudo-random number generator just not evenly distributed enough as to generate this 200 times difference?</p>

<p>I know how to work around this and I'm aware that such small odds shouldn't even be considered for every day's uses, but I'd love to understand what is going on here and if my math is broken or Mozilla's (I hope it is former). <code>=]</code> Any input is appreciated.</p>
