praxis

Various programming exercises.
git clone git://git.jtobin.io/praxis.git
Log | Files | Refs

altmonty.hs (656B)


      1 import System.Random.MWC
      2 import Control.Monad
      3 
      4 data Choice = Switch | Stay deriving Eq
      5 
      6 montyRandSwitch g = do
      7     z0     <- uniformR (0, 2) g        :: IO Int                    
      8     switch <- uniformR (False, True) g :: IO Bool
      9     return $ if switch 
     10              then (z0 /= 2, Switch)
     11              else (z0 == 2, Stay)
     12 
     13 runAltSimulation n g = do
     14      ws <- liftM (filter fst) (replicateM n (montyRandSwitch g))
     15      let winBySwitch = length . filter ((== Switch) . snd) $ ws
     16          winByStay   = length . filter ((== Stay)   . snd) $ ws
     17      return (winBySwitch, winByStay)
     18 
     19 main = do 
     20     g <- create 
     21     replicateM 10 (runAltSimulation 10000 g)
     22