Created
January 5, 2012 07:11
-
-
Save nushio3/1564097 to your computer and use it in GitHub Desktop.
サイコロを振るモナド ref: http://qiita.com/items/1579
This file contains hidden or 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
{-# OPTIONS -Wall #-} | |
import Control.Monad | |
import Control.Monad.Random | |
import Data.List | |
sumDice :: (MonadRandom m) => Int -> m Int | |
sumDice n = liftM sum $ replicateM n $ getRandomR (1,6) | |
statDice :: (MonadRandom m) => Int -> m [(Int,Int)] | |
statDice n = do | |
liftM histogram $ replicateM n $ sumDice 2 | |
where | |
histogram :: (Ord x) => [x] -> [(x,Int)] | |
histogram = map (\xs -> (head xs, length xs)) . group . sort | |
pureStat :: [(Int, Int)] | |
pureStat = evalRand (statDice 360000) (mkStdGen 890135) | |
main :: IO () | |
main = do | |
print pureStat | |
statDice 360000 >>= print |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment