Skip to content

Instantly share code, notes, and snippets.

@fgrsnau
Last active September 8, 2015 10:56
Show Gist options
  • Save fgrsnau/1bcd332b920b4410bd73 to your computer and use it in GitHub Desktop.
Save fgrsnau/1bcd332b920b4410bd73 to your computer and use it in GitHub Desktop.
{-# 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