Skip to content

Instantly share code, notes, and snippets.

@octopuscabbage
Last active August 29, 2015 14:15
Show Gist options
  • Save octopuscabbage/a5d0296143c0010b54e8 to your computer and use it in GitHub Desktop.
Save octopuscabbage/a5d0296143c0010b54e8 to your computer and use it in GitHub Desktop.
1-5 to 1-7 generator
import System.Random
import Control.Monad
main = do
let trials = 100000
vals ← collect trials $ zip [1‥7] $ cycle [0]
let percents = map (λ(value,amount) → (value,amount / trials)) vals
print percents
--collect:: Int -> [(Int, Int)] -> IO [(Int, Int)]
collect 0 xs = return xs
collect n xs = runRandom »= λr → collect (n-1) (addOneToValue r)
where addOneToValue v = foldl (λys t@(value,amount) → if value ≡ v then (value,amount+1):ys else t:ys) [] xs
runRandom = do
vals ← mapM (fake (randomRIO (1,5)∷IO Int)) [1‥7]
let summed = sum vals
return $ (summed `mod` 7)+1
fake f = \_ -> f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment