Skip to content

Instantly share code, notes, and snippets.

@luqui
Created February 11, 2009 21:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luqui/62284 to your computer and use it in GitHub Desktop.
Save luqui/62284 to your computer and use it in GitHub Desktop.
import Control.Monad.Random
import Control.Monad.Writer
type Sim = Rand StdGen
-- returns the series of rolls, with the final 6 omitted (implied)
simGame :: Sim [Int]
simGame = do
roll <- getRandomR (1,6)
if roll == 6
then return []
else fmap (roll:) simGame
-- bob wins if there were an odd number of rolls before a 6
bobWins :: [Int] -> Bool
bobWins = odd . length
bobWinsSecondTurn :: [Int] -> Bool
bobWinsSecondTurn rolls = length rolls == 3
-- returns a list of ever-refining probabilities
simulate :: Sim [Double]
simulate = execWriterT (go 0 0)
where
go tries successes = do
tell [successes/tries]
game <- lift simGame
go (inc (bobWins game) tries) (inc (bobWinsSecondTurn game) successes)
-- converts a bool into an incrementor function
-- (if true, adds one, else leaves alone)
inc True = (+1)
inc False = id
main = mapM_ print =<< fmap (evalRand simulate) newStdGen
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment