[Haskell-beginners] randmomR produces only even values
maydwell at gmail.com
Fri Oct 30 22:40:24 UTC 2015
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.
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]
Still, I am very surprised by this behaviour. I couldn't find any reference
to this behaviour in, which is supposedly what the System.Random
implementation is based on.
Does anyone else have an explanation?
 - https://en.wikipedia.org/wiki/Linear_congruential_generator
On Sat, Oct 31, 2015 at 5:43 AM, martin <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
> 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)
> I get 499 (okay)
> Why is that so?
> Beginners mailing list
> Beginners at haskell.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Beginners