Send Beginners mailing list submissions to beginners@haskell.org To subscribe or unsubscribe via the World Wide Web, visit http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners or, via email, send a message with subject or body 'help' to beginners-requ...@haskell.org
You can reach the person managing the list at beginners-ow...@haskell.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Beginners digest..." Today's Topics: 1. randmomR produces only even values (martin) 2. Re: randmomR produces only even values (Lyndon Maydwell) 3. Re: randmomR produces only even values (martin) ---------------------------------------------------------------------- Message: 1 Date: Fri, 30 Oct 2015 19:43:23 +0100 From: martin <martin.drautzb...@web.de> To: beginners@haskell.org Subject: [Haskell-beginners] randmomR produces only even values Message-ID: <5633ba4b.7030...@web.de> Content-Type: text/plain; charset=utf-8 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? ------------------------------ Message: 2 Date: Sat, 31 Oct 2015 09:40:24 +1100 From: Lyndon Maydwell <maydw...@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] randmomR produces only even values Message-ID: <CAM5QZtzeQcT0QOOQbQuexNAqKNgQUTkOHB4aGQwk=ffhrri...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" 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.drautzb...@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@haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20151031/4b9aff32/attachment-0001.html> ------------------------------ Message: 3 Date: Sat, 31 Oct 2015 09:32:58 +0100 From: martin <martin.drautzb...@web.de> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] randmomR produces only even values Message-ID: <56347cba.8030...@web.de> Content-Type: text/plain; charset=windows-1252 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.drautzb...@web.de > <mailto:martin.drautzb...@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@haskell.org <mailto:Beginners@haskell.org> > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > > > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > ------------------------------ Subject: Digest Footer _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners ------------------------------ End of Beginners Digest, Vol 88, Issue 28 *****************************************