fork download
  1. {-# LANGUAGE BangPatterns #-}
  2. import qualified Data.IntMap.Lazy as I
  3. import Control.Monad
  4. import System.Random
  5.  
  6. main :: IO ()
  7. main = do
  8. (a, b) <- dicePi 4 1000000
  9. putStrLn $ "pi = " ++ show a
  10.  
  11. dicePi :: Int -> Double -> IO (Double, [(Int, Int)])
  12. dicePi diceNum n = do
  13. (r, d) <- go n 0 (I.fromList $ zip [1..6] [0,0..])
  14. return (4 * r / n, I.assocs d)
  15. where
  16. go 0 count d = return (count, d)
  17. go m count d = do
  18. x0 <- replicateM diceNum rollDice
  19. y0 <- replicateM diceNum rollDice
  20. let !h = (sqrt $! x * x + y * y) :: Double
  21. !x = f x0
  22. !y = f y0
  23. d' = foldr upd d $ x0 ++ y0
  24. go (m - 1) (if h < 1.0 then count + 1 else count) d'
  25. f :: [Int] -> Double
  26. f xxs = (/ (6 ^ diceNum)) $ fromIntegral $ g xxs
  27. where
  28. g [] = 0
  29. g (x:xs) = x - 1 + 6 * (g xs)
  30. upd k im = I.update ((Just $!) . succ) k im
  31.  
  32. rollDice :: IO Int
  33. rollDice = getStdRandom (randomR (1, 6))
  34.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.hs:4:8:
    Could not find module `System.Random'
    Use -v to see a list of the files searched for.
stdout
Standard output is empty