[Haskell-cafe] Infelicity in StdGen?

Rob Arthan rda at lemma-one.com
Fri Jan 3 16:28:28 UTC 2014


Either I am misunderstanding something or there is an infelicity
in the implementation of StdGen. The documentation on mkStdGen says
that distinct arguments should be likely to produce distinct generators.
This made me think that I would get a reasonable pseudo-random function
to simulate n rolls of a die by using n to seed the random number generator:

import System.Random
roll :: Int -> String
roll n = take n . randomRs ('1', '6') . mkStdGen $ n

However, this produces a string beginning with a '6' for 0 <= n <= 53667.
In fact the dependency of the first value on the seed seems to be far from
random:

map (\l -> (head l, length l)) . group . map (fst . randomR (1, 6) . mkStdGen) $ [0..25*53668+6]

returns:

[(6,53668),(5,53668),(4,53668),(3,53669),(2,53668),(1,53668),(6,53669),(5,53668),(4,53668),(3,53669),(2,53668),(1,53668),(6,53668),(5,53669),(4,53668),(3,53668),(2,53669),(1,53668),(6,53668),(5,53669),(4,53668),(3,53668),(2,53669),(1,53668),(6,53668)]

The behaviour seems to be related to the length of the range.
You get similar behaviour for ranges of length 2, 3, 6 and 9 for example,
but not for 4, 5, 7 or 8.

If it is relevant, I am using ghc version 7.6.3.

Regards,

Rob.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20140103/8411392c/attachment.html>


More information about the Haskell-Cafe mailing list