[GHC] #5280: System.Random commits (rand `mod` base) error.
GHC
cvs-ghc at haskell.org
Mon Jun 27 04:41:23 CEST 2011
#5280: System.Random commits (rand `mod` base) error.
---------------------------------+------------------------------------------
Reporter: rrnewton | Owner: rrnewton
Type: bug | Status: new
Priority: normal | Component: libraries/random
Version: 7.0.3 | Keywords: random mod base
Testcase: | Blockedby:
Os: Unknown/Multiple | Blocking:
Architecture: Unknown/Multiple | Failure: Incorrect result at runtime
---------------------------------+------------------------------------------
You have probably at some point come across the C code "{{{rand() %
base}}}"'. It is very intuitive, but unfortunately creates non-uniform
random numbers, which is easy to see if you imagine {{{rand()}}} producing
numbers in say `[0,15)` and base being `10`.
In the function `System.Random.randomIvalInteger` you can see the same
thing happening.
The only way I know how to deal with it and generate uniform integers
within a range is to artificially restrict the range of the source of
randomness to be a multiple of the desired base. It can be done simply by
throwing out some random results.
This strategy appears to be used by GMP's mpz_urandomm function:
http://gmplib.org/manual/Integer-Random-Numbers.html#Integer-Random-
Numbers
The file `urandomm.c` has the code.
--
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/5280>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler
More information about the Glasgow-haskell-bugs
mailing list