Last active
September 8, 2015 10:56
-
-
Save fgrsnau/1bcd332b920b4410bd73 to your computer and use it in GitHub Desktop.
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
{-# LANGUAGE NumDecimals #-} | |
module Main where | |
import Data.List | |
import Data.List.Split | |
import Data.Ratio | |
import System.Random | |
data Coin = Head | Tail deriving (Eq, Enum, Read, Show) | |
instance Random Coin where | |
randomR (lo, hi ) g = let (x, g') = randomR (fromEnum lo, fromEnum hi) g | |
in (toEnum x, g') | |
random = randomR (Head, Tail) | |
main :: IO () | |
main = do | |
coins <- randoms <$> newStdGen | |
let trials = splitWhen (== Head) coins | |
let outcomes = product . map (const (2 :: Integer)) <$> trials | |
let average = let n = 1e7 in sum (take n outcomes) % (fromIntegral n) | |
print (fromRational average) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment