# Ticket #3620: RandomnessTest2.hs

File RandomnessTest2.hs, 1.2 KB (added by NickSmallbone, 4 years ago)
Line
1module Main where
2
4import System.Random
5
6splitN :: (RandomGen g) => Int -> g -> ([g], g)
7splitN 0 g = ([], g)
8splitN n g = (g1:l, g')
9  where
10  (l, g') = splitN (n-1) g2
11  (g1, g2) = split g
12
13-- The funny splitting operation.
14split' :: (RandomGen g) => g -> (g, g)
15split' g = (g12, g21)
16  where
17  (g1, g2) = split g
18  (_, g12) = split g1
19  (g21, _) = split g2
20
21-- This test checks if generators created by calling split 2 times are independent.
22-- It generates pairs of integers from 0 to n-1, using split' to
23-- generate both numbers using one seed. Then it counts how often the
24-- two numbers are equal.
25test :: (RandomGen g) => Int -> Int -> g -> Int
26test numTests n g = equals
27  where
28  (gs, _) = splitN numTests g
29  equals = count id \$ map single gs
30  count p l = length \$ filter p l
31  single g' = (fst \$ randomR (0, n-1) g1) == (fst \$ randomR (0, n-1) g2)
32    where
33    (g1, g2) = split' g'
34
35main = do
36  g <- newStdGen
37  forM_ [2..15] \$ \i -> do
38    let actual = test (i * 1000) i g
39    putStrLn \$ "Generated " ++ show (i * 1000)
40      ++ " pairs of numbers from 0 to " ++ show (i - 1)
41      ++ " -- " ++ show actual ++ " pairs contained equal numbers "
42      ++ "and we expected about 1000."