[GHC] #5133: Random instance for Float can generate values out of requested range
GHC
cvs-ghc at haskell.org
Fri Jun 24 18:45:56 CEST 2011
#5133: Random instance for Float can generate values out of requested range
---------------------------------+------------------------------------------
Reporter: richardsenington | Owner: rrnewton
Type: bug | Status: new
Priority: normal | Milestone:
Component: libraries/base | Version: 7.1
Keywords: System.Random | Testcase:
Blockedby: | Difficulty:
Os: Linux | Blocking:
Architecture: x86 | Failure: None/Unknown
---------------------------------+------------------------------------------
Comment(by scpmw):
Well, the way this was discovered was a program assuming `floor ((fst
(random gen) :: Float) * fromIntegral x) < x`. That might well be why
other languages have chosen this approach as well.
I read that paper as well after commenting... The interesting question is
how often the extra bits of information would actually have a positive
effect in programs. And whether it's worth it to have the algorithm loop a
few times for that.
In my humble opinion, the cleanest solution would be to determine how many
fraction bits we need (e.g. Float -> 23, Double -> 52). Then we can
generate a random number of the appropriate range, which maps 1:1 to a
floating-point representation. Not *all* possible floating point
representations, obviously, but at least it would be fast, uniform and in
the [0, 1) range.
--
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/5133#comment:8>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler
More information about the Glasgow-haskell-bugs
mailing list