Created
February 11, 2009 21:40
-
-
Save luqui/62284 to your computer and use it in GitHub Desktop.
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
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