Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import System.Random
  2.  
  3. maxC = 23
  4.  
  5. data CardState = CardState { num0  :: Int
  6.                            , num1  :: Int
  7.                            , cards :: [Int]
  8.                            } deriving (Show)
  9.  
  10. makeList :: [Int] -> CardState -> CardState
  11. makeList (x:xs) n | n0 >= maxC && n1 >= maxC = n
  12.                   | n0 >= maxC && x == 0    = makeList xs n
  13.                   | n1 >= maxC && x == 1    = makeList xs n
  14.                   | x == 0 = makeList xs $ CardState (n0 + 1) n1 (cs ++ [x])
  15.                   | x == 1 = makeList xs $ CardState n0 (n1 + 1) (cs ++ [x])
  16.   where n0 = num0 n
  17.         n1 = num1 n
  18.         cs = cards n
  19.  
  20. checkWin (x:xs) | length xs <= 2         = x == 0
  21.                 | x == 1 && xs !! 1 == 1 = xs !! 2 == 0
  22.                 | otherwise              = checkWin xs
  23.  
  24. runTrial = do g <- newStdGen
  25.               let xs  = randomRs (0,1) g :: [Int]
  26.               let xs' = makeList xs (CardState 0 0 [])
  27.              return $ checkWin (cards xs')
  28.  
  29. main = do xs <- mapM (\x -> runTrial) [1..10000]
  30.           print $ fromIntegral (length (filter (==True) xs)) / fromIntegral (length xs)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement