Skip to content

Instantly share code, notes, and snippets.

@nushio3
Created January 5, 2012 07:11
Show Gist options
  • Save nushio3/1564097 to your computer and use it in GitHub Desktop.
Save nushio3/1564097 to your computer and use it in GitHub Desktop.
サイコロを振るモナド ref: http://qiita.com/items/1579
{-# 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