[GHC] #5133: Random instance for Float can generate values out of requested range
GHC
cvs-ghc at haskell.org
Tue Jun 21 20:14:00 CEST 2011
#5133: Random instance for Float can generate values out of requested range
---------------------------------+------------------------------------------
Reporter: richardsenington | Owner:
Type: bug | Status: new
Priority: normal | Component: libraries/base
Version: 7.1 | Keywords: System.Random
Testcase: | Blockedby:
Os: Linux | Blocking:
Architecture: x86 | Failure: None/Unknown
---------------------------------+------------------------------------------
Changes (by wgmitchener):
* cc: garrett.mitchener@… (added)
Comment:
I ran into this a while back and forgot to post a bug-- sorry. It's
nothing so exotic. If you go down to the function randomIvalDouble in
Random.hs line 400 or so (and I'm looking at the one from
http://darcs.haskell.org/packages/random), here's where something is going
wrong:
case (randomIvalInteger (toInteger (minBound::Int32), toInteger
(maxBound::Int32)) rng) of
(x, rng') ->
let
scaled_x =
fromDouble ((l+h)/2) +
fromDouble ((h-l) / realToFrac int32Range) *
fromIntegral (x::Int32)
in
(scaled_x, rng')
...
int32Range = toInteger (maxBound::Int32) - toInteger (minBound::Int32)
But if the pseudo-random integer x happens to be minBound, which is
-2^31^, then the scaled_x comes out
1/2 - 2^31^ / (2^32^ - 1) = 2^31^ ( 1/2^32^ - 1/(2^32^ - 1) ) =
-1.164e-10 < 0
which is outside the required range. An easy fix might be to add 1 to the
min bound used to generate x.
--
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/5133#comment:4>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler
More information about the Glasgow-haskell-bugs
mailing list