<div><font class="Apple-style-span" face="arial, helvetica, sans-serif">Hi, I&#39;m having some trouble with the standard random number generator.  I re-implemented it for speed, but the same problem seems to be present in the original.  The problem I&#39;m having is in the generation of a random Double.  If you go down to the function randomIvalDouble in Random.hs (and I&#39;m looking at the one from <a href="http://darcs.haskell.org/packages/random">http://darcs.haskell.org/packages/random</a>), here&#39;s where something is going wrong:</font></div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><br></font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">       case (randomIvalInteger (toInteger (minBound::Int32), toInteger (maxBound::Int32)) rng) of</font></div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">         (x, rng&#39;) -&gt; </font></div><div><span class="Apple-tab-span" style="white-space:pre"><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">        </font></span><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">    let</font></div>

<div><span class="Apple-tab-span" style="white-space:pre"><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">        </font></span><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">     scaled_x = </font></div>

<div><span class="Apple-tab-span" style="white-space:pre"><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">                </font></span><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">fromDouble ((l+h)/2) + </font></div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">                fromDouble ((h-l) / realToFrac int32Range) *</font></div><div><span class="Apple-tab-span" style="white-space:pre"><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">                </font></span><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">fromIntegral (x::Int32)</font></div>

<div><span class="Apple-tab-span" style="white-space:pre"><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">        </font></span><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">    in</font></div>

<div><span class="Apple-tab-span" style="white-space:pre"><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">        </font></span><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">    (scaled_x, rng&#39;)</font></div>

<div><br></div><div>...</div><div><span class="Apple-style-span" style="font-family: &#39;courier new&#39;, monospace; ">int32Range = toInteger (maxBound::Int32) - toInteger (minBound::Int32)</span></div><div><br></div><div>

I use essentially the same calculation in my implementation with l = 0 and h = 1, that is, I want to generate a random Double in the interval [0,1].  int32Range comes out to be 2^32-1.  But if the pseudo-random integer x happens to be minBound, which is -2^31, then the scaled_x comes out</div>

<div><br></div><div>1/2 - 2^31/(2^32-1)  = 2^31( 1/2^32 - 1/(2^32-1) ) = -1.164e-10 &lt; 0</div><div><br></div><div>which is outside the required range.  One of my simulations, which has been running for weeks, actually hit this, and I&#39;d like to know if I&#39;m just missing something or if this is a real bug.  It breaks my simulation because it triggers an attempt to access element -1 of an array whose minimum index is 0, by the way.</div>

<div><br></div><div>My trouble at this point is that I&#39;d really like for my simulation results to be completely reproduceable, and I&#39;m going to be unhappy if I have to re-implement the random generation and change it so that I have to throw out all the results I have so far.  I&#39;m also going to be unhappy if I have to insert some check that slows everything down...</div>

<div><br></div><div>At any rate, if I&#39;m right, this bug affects GHC and Hugs.  But this code has surely been around long enough that someone would have found this already?  But there&#39;s not a bug in hackage&#39;s trac system.</div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><br></font></div><div><font class="Apple-style-span" face="arial, helvetica, sans-serif">Help!</font></div><div><font class="Apple-style-span" face="arial, helvetica, sans-serif"><br>

</font></div><div><font class="Apple-style-span" face="arial, helvetica, sans-serif">-- Garrett Mitchener</font></div>