<div><font class="Apple-style-span" face="arial, helvetica, sans-serif">Hi, I'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'm having is in the generation of a random Double. If you go down to the function randomIvalDouble in Random.hs (and I'm looking at the one from <a href="http://darcs.haskell.org/packages/random">http://darcs.haskell.org/packages/random</a>), here's where something is going wrong:</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"><br></font></div><div><font class="Apple-style-span" face="'courier new', monospace"> case (randomIvalInteger (toInteger (minBound::Int32), toInteger (maxBound::Int32)) rng) of</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> (x, rng') -> </font></div><div><span class="Apple-tab-span" style="white-space:pre"><font class="Apple-style-span" face="'courier new', monospace">        </font></span><font class="Apple-style-span" face="'courier new', monospace"> let</font></div>
<div><span class="Apple-tab-span" style="white-space:pre"><font class="Apple-style-span" face="'courier new', monospace">        </font></span><font class="Apple-style-span" face="'courier new', monospace"> scaled_x = </font></div>
<div><span class="Apple-tab-span" style="white-space:pre"><font class="Apple-style-span" face="'courier new', monospace">                </font></span><font class="Apple-style-span" face="'courier new', monospace">fromDouble ((l+h)/2) + </font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> fromDouble ((h-l) / realToFrac int32Range) *</font></div><div><span class="Apple-tab-span" style="white-space:pre"><font class="Apple-style-span" face="'courier new', monospace">                </font></span><font class="Apple-style-span" face="'courier new', monospace">fromIntegral (x::Int32)</font></div>
<div><span class="Apple-tab-span" style="white-space:pre"><font class="Apple-style-span" face="'courier new', monospace">        </font></span><font class="Apple-style-span" face="'courier new', monospace"> in</font></div>
<div><span class="Apple-tab-span" style="white-space:pre"><font class="Apple-style-span" face="'courier new', monospace">        </font></span><font class="Apple-style-span" face="'courier new', monospace"> (scaled_x, rng')</font></div>
<div><br></div><div>...</div><div><span class="Apple-style-span" style="font-family: 'courier new', 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 < 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'd like to know if I'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'd really like for my simulation results to be completely reproduceable, and I'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'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'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's not a bug in hackage's trac system.</div>
<div><font class="Apple-style-span" face="'courier new', 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>