Ticket #3575: random-seed.hs

File random-seed.hs, 433 bytes (added by rwbarton, 5 years ago)

Alternative example program which reads a seed from /dev/urandom

Line 
1import Control.Monad
2import System.Random
3import System.IO
4
5print0or1 = do
6  g <- newStdGen
7  putStr . show . fst $ randomR (0, 1 :: Int) g
8
9main = do
10  -- These four lines read an initial seed from /dev/urandom.
11  f <- openBinaryFile "/dev/urandom" ReadMode
12  xs <- replicateM 4 $ hGetChar f
13  let seed = sum $ zipWith (*) (iterate (* 256) 1) (map fromEnum xs)
14  setStdGen $ mkStdGen seed
15
16  replicateM_ 30 print0or1 >> putStrLn ""