Skip to content

Instantly share code, notes, and snippets.

@ishiy1993
Created October 21, 2015 01:05
Show Gist options
  • Save ishiy1993/20d37a1395ca7d15262d to your computer and use it in GitHub Desktop.
Save ishiy1993/20d37a1395ca7d15262d to your computer and use it in GitHub Desktop.
Haskellでモンテカルロ積分
import System.Random
import Data.List
main :: IO ()
main = do
xGen <- getStdGen
yGen <- newStdGen
let n = 100000
let xs = take n $ randomRs (0,1) xGen
ys = take n $ randomRs (0,1) yGen
print $ calPi n xs ys
calPi :: Int -> [Double] -> [Double] -> Double
calPi n xs ys = 4 * count / (fromIntegral n)
where count = foldl' (+) 0 $ map cond $ zip xs ys
cond :: (Double,Double) -> Double
cond (x,y) | x**2 + y**2 < 1 = 1
| otherwise = 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment