Created
February 6, 2011 17:05
-
-
Save jb55/813513 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Control.Arrow | |
import Control.Monad | |
import Control.Monad.Random | |
import Data.List | |
import Data.Map | |
import Data.Maybe | |
-- A monad which represents a random integer from 1-6 | |
die :: (RandomGen g) => Rand g Int | |
die = getRandomR (1,6) | |
-- A monad which represents our infinite list of summed paired die | |
dice :: (RandomGen g) => Rand g [Int] | |
dice = sequence . repeat . summedTuple $ pair | |
where summedTuple = liftM (\(a, b) -> a + b) | |
-- A roll of 2 dice | |
pair :: (RandomGen g) => Rand g (Int, Int) | |
pair = do | |
r1 <- die | |
r2 <- die | |
return (r1, r2) | |
-- Build a histogram of a list of objects | |
hist :: (Ord a) => [a] -> [(a, Int)] | |
hist = map (head &&& length) . group . sort | |
-- Take stuff from a list in a monad | |
takeM n = liftM (take n) | |
-- Build a histogram of our random list | |
randHist n = (liftM hist) . takeM n | |
diceHist n = randHist n dice | |
-- The probability distribution of a paired dice roll | |
diceDist :: [(Int, Rational)] | |
diceDist = [(2, 1/36), (3, 2/36), (4, 3/36), (5, 4/36), (6, 5/36), (7, 6/36), | |
(8, 5/36), (9, 4/36), (10, 3/36), (11, 2/36), (12, 1/36)] | |
initFreqs :: [(Int, Int)] | |
initFreqs = [ (x, 0) | x <- [2..12] ] | |
main = do | |
print =<< (evalRandIO $ diceHist 100000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment