Created
September 17, 2014 20:40
-
-
Save ucarion/de3f5e22c2ec373d9a51 to your computer and use it in GitHub Desktop.
Pig score distributions
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
-- Execute this as: | |
-- | |
-- ghc -O2 hog.hs && ./hog | |
-- | |
-- Which takes about 1 minute to execute. | |
import Data.List (any) | |
import Control.Monad (replicateM) | |
rolls :: Int -> Int -> [[Int]] | |
rolls numDice numFaces = replicateM numDice [1..numFaces] | |
score :: [Int] -> Int | |
score xs = if any (== 1) xs then 1 else sum xs | |
distribScores :: Int -> Int -> [Float] | |
distribScores numDice numFaces = | |
map normalize scoreCounts | |
where | |
normalize :: Int -> Float | |
normalize n = (fromIntegral n) / totalCount | |
scoreCounts :: [Int] | |
scoreCounts = map (\n -> length $ filter (== n) scores) [1..maxScore] | |
totalCount :: Num a => a | |
totalCount = fromIntegral $ length scores | |
scores :: [Int] | |
scores = map score $ rolls numDice numFaces | |
maxScore :: Int | |
maxScore = numFaces * numDice | |
main :: IO () | |
main = do | |
putStrLn "Distrib scores for [1..10] 4-sided dice:" | |
print $ map (\n -> distribScores n 4) [1..10] | |
putStrLn "" | |
putStrLn "Distrib scores for [1..10] 6-sided dice:" | |
print $ map (\n -> distribScores n 6) [1..10] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment