[Haskell-cafe] New seed. New random number. Right? Wrong.
Peter Verswyvelen
bugfact at gmail.com
Thu Oct 8 16:29:21 EDT 2009
interesting.
the sequences you get are random, but unless you enter a new number that is
really far from the previous one, the probability of getting the same first
random number seems high.
import System.Random
import Control.Monad(when)
main = do
numberString <- getLine
when (not $ null numberString) $ do
let seed = read numberString
let randNumbers = take 10 . randomRs (1::Int,6) $ mkStdGen seed
in putStrLn $ "Numbers are " ++ show randNumbers
main
1
Numbers are [6,5,2,6,5,2,3,2,5,5]
1000
Numbers are [6,4,3,2,6,5,2,3,3,5]
1000000
Numbers are [6,4,3,6,5,5,3,4,6,4]
1000000000
Numbers are [4,1,3,4,6,1,2,4,1,6]
1000000000000
Numbers are [1,2,3,3,5,5,4,6,6,4]
1000000000000000
Numbers are [1,4,2,6,6,2,4,2,6,5]
On Thu, Oct 8, 2009 at 10:15 PM, michael rice <nowgate at yahoo.com> wrote:
> I'm entering a new number to make a different seed for my generator each
> time through the loop, but my resulting numbers are anything but random.
>
> Where am I going wrong?
>
> Michael
>
> =============================
>
> import System.Random
> import Control.Monad(when)
>
> main = do
> numberString <- getLine
> when (not $ null numberString) $ do
> let seed = read numberString
> let (randNumber, newGen) = randomR (1,6) (mkStdGen seed) :: (Int,
> StdGen)
> in putStrLn $ "Number is " ++ show randNumber
> main
>
> =============================
>
> [michael at localhost ~]$ runhaskell zz.hs
> 12317
> Number is 6
> 12371
> Number is 6
> 37829
> Number is 6
> 8713
> Number is 6
> 71392
> Number is 5
> 29492
> Number is 6
> 41089
> Number is 6
> 28483
> Number is 6
> 78687
> Number is 5
> 40703
> Number is 6
> 86910
> Number is 5
> 15608
> Number is 6
> 56155
> Number is 5
> 31575
> Number is 6
> 72231
> Number is 5
> 51815
> Number is 6
> 20189
> Number is 6
> 71533
> Number is 5
> 17755
> Number is 6
> 91161
> Number is 5
>
> [michael at localhost ~]$
>
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20091008/8a902937/attachment.html
More information about the Haskell-Cafe
mailing list