[Haskell-beginners] randmomR produces only even values

martin martin.drautzburg at web.de
Sat Oct 31 08:32:58 UTC 2015


I filed an issue on github (https://github.com/haskell/random/issues). Maybe the authors can shed some light on this.

Am 10/30/2015 um 11:40 PM schrieb Lyndon Maydwell:
> I've replicated this and it does seem very strange, and possibly even a bug.
> 
> I would guess that most people don't encounter this issue as a generator is usually only seeded once, then threaded
> throughout generation. Not seeded for once for every random output. The other common practice is that generators are
> usually seeded on far more random input values than a list of ascending ints.
> 
> Seed behaviour:
> 
>     main = mapM_ print (map p l)
>     p  x = length $ filter even $ map (\s -> fst $ randomR (1::Int,10) (mkStdGen s)) x
>     l    = map ls [1..10]
>     ls x = map (\y -> x * y * 10) [1..1000]
> 
>     1000
>     1000
>     1000
>     1000
>     1000
>     894
>     766
>     670
>     596
>     536
> 
> 
> Still, I am very surprised by this behaviour. I couldn't find any reference to this behaviour in[1], which is supposedly
> what the System.Random implementation is based on.
> 
> Does anyone else have an explanation?
> 
> 
>  - Lyndon
> 
> 
> [1] - https://en.wikipedia.org/wiki/Linear_congruential_generator
> 
> On Sat, Oct 31, 2015 at 5:43 AM, martin <martin.drautzburg at web.de <mailto:martin.drautzburg at web.de>> wrote:
> 
>     Hello all
> 
> 
>     When I read:
> 
>     mkStdGen :: Int -> StdGen
> 
>     The function mkStdGen provides an alternative way of producing an initial generator, by mapping an Int into a generator.
>     Again, distinct arguments should be likely to produce distinct generators.
> 
>     I thought, that
> 
>     > fst $ R.randomR (1,10) (R.mkStdGen s)
> 
>     should get me a random value between 1 and 10 and that I get different values depending on the seed s. But this
> 
>     > length $ filter even $ map (\s -> fst $ randomR (1::Int,10) (mkStdGen s))[1..1000]
> 
>     gives my 1000, i.e. all random numbers are even numbers.
> 
>     However, when I use random instead of randomR
> 
>     > length $ filter even $ map (\s -> fst $ random (mkStdGen s) ::Int)[1..1000]
> 
>     I get 499 (okay)
> 
>     Why is that so?
>     _______________________________________________
>     Beginners mailing list
>     Beginners at haskell.org <mailto:Beginners at haskell.org>
>     http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
> 
> 
> 
> 
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
> 



More information about the Beginners mailing list