Skip to content

System.Random.randomIvalInteger makes invalid assumptions about RandomGen

The existing API for System.Random.RandomGen allows a random number generator (RNG) to produce Ints within an arbitrary range specified by genRange.

For example, the following RandomGen produces only zeros and ones, but should be legitimate:

import System.Random

data BinRNG = BinRNG StdGen
instance RandomGen BinRNG where 
  next (BinRNG g) = (x `mod` 2, BinRNG g')
    where (x,g') = next g
  split (BinRNG g) = (BinRNG g1, BinRNG g2)
    where (g1,g2) = split g
  genRange _ = (0,1)

ls :: [Int]
ls = randoms (BinRNG$ mkStdGen 38388)

main = print $ take 20 ls

But System.Random.randomIvalInteger makes invalid assumptions about the amount of randomness produced by next. (Specifically, assuming that it creates the same amount as StdGen.) Thus, the above program will create an output with only a couple of unique ints (rather than 2^64).

For example:

[4611734781337924537,4611734781337924537,-9223323645458902796,
-9223323645458902797,4611734783485408099,4611734783485408098,
-9223323645458902796,-9223323647606386357,4611734781337924538,
-9223323645458902796,-9223323645458902797,
-9223323647606386357,4611734783485408098,4611734783485408098,
-9223323647606386357,4611734781337924538,4611734781337924537,
-9223323645458902796,4611734783485408099,4611734781337924538]
Trac metadata
Trac field Value
Version 7.0.3
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component libraries/random
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information