Skip to content

Commit

Permalink
Add some tests demonstrating #25.
Browse files Browse the repository at this point in the history
  • Loading branch information
idontgetoutmuch committed Apr 4, 2015
1 parent cfdfe6f commit 5af0c12
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
42 changes: 42 additions & 0 deletions Benchmark/RandomnessTest2.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
module Main where

import Control.Monad
import System.Random

-- splitN :: (RandomGen g) => Int -> g -> ([g], g)
splitN 0 g = ([], g)
splitN n g = (g1:l, g')
where
(l, g') = splitN (n-1) g2
(g1, g2) = split g

-- The funny splitting operation.
split' :: (RandomGen g) => g -> (g, g)
split' g = (g12, g21)
where
(g1, g2) = split g
(_, g12) = split g1
(g21, _) = split g2

-- This test checks if generators created by calling split 2 times are independent.
-- It generates pairs of integers from 0 to n-1, using split' to
-- generate both numbers using one seed. Then it counts how often the
-- two numbers are equal.
test :: (RandomGen g) => Int -> Int -> g -> Int
test numTests n g = equals
where
(gs, _) = splitN numTests g
equals = count id $ map single gs
count p l = length $ filter p l
single g' = (fst $ randomR (0, n-1) g1) == (fst $ randomR (0, n-1) g2)
where
(g1, g2) = split' g'

main = do
let g = mkStdGen 42
forM_ [2..15] $ \i -> do
let actual = test (i * 1000) i g
putStrLn $ "Generated " ++ show (i * 1000)
++ " pairs of numbers from 0 to " ++ show (i - 1)
++ " -- " ++ show actual ++ " pairs contained equal numbers "
++ "and we expected about 1000."
43 changes: 43 additions & 0 deletions Benchmark/RandomnessTest3.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module Main where

import Control.Monad
import System.Random.TF.Gen
import System.Random.TF.Instances

splitN :: (RandomGen g) => Int -> g -> ([g], g)
splitN 0 g = ([], g)
splitN n g = (g1:l, g')
where
(l, g') = splitN (n-1) g2
(g1, g2) = split g

-- The funny splitting operation.
split' :: (RandomGen g) => g -> (g, g)
split' g = (g12, g21)
where
(g1, g2) = split g
(_, g12) = split g1
(g21, _) = split g2

-- This test checks if generators created by calling split 2 times are independent.
-- It generates pairs of integers from 0 to n-1, using split' to
-- generate both numbers using one seed. Then it counts how often the
-- two numbers are equal.
test :: (RandomGen g) => Int -> Int -> g -> Int
test numTests n g = equals
where
(gs, _) = splitN numTests g
equals = count id $ map single gs
count p l = length $ filter p l
single g' = (fst $ randomR (0, n-1) g1) == (fst $ randomR (0, n-1) g2)
where
(g1, g2) = split' g'

main = do
let g = seedTFGen (42, 42, 42, 42)
forM_ [2..15] $ \i -> do
let actual = test (i * 1000) i g
putStrLn $ "Generated " ++ show (i * 1000)
++ " pairs of numbers from 0 to " ++ show (i - 1)
++ " -- " ++ show actual ++ " pairs contained equal numbers "
++ "and we expected about 1000."

0 comments on commit 5af0c12

Please sign in to comment.