import System.Random import qualified Data.Map as Map import Data.Map (Map) import Data.List import Control.Monad.State simulate count m n = do mapM (dice m) (replicate count n) histogram :: Ord a => [a] -> [(a,Int)] histogram = Map.assocs . foldl f Map.empty where f m k = Map.insertWith (+) k 1 m simulation = simulate 1000000 3 6 >>= return . histogram main = run {- dice :: Int -> Int -> IO Int dice 0 n = return 0 dice m n = do total <- dice (m - 1) n roll <- randomRIO (1, n) return (total + roll) run = simulation >>= putStrLn . show -} die :: RandomGen g => Int -> State g Int die n = State $ randomR (1,n) dice :: RandomGen g => Int -> Int -> State g Int dice m n = liftM sum $ replicateM m (die n) run = getStdGen >>= return . evalState simulation >>= putStrLn . show