Created
October 21, 2015 01:05
-
-
Save ishiy1993/20d37a1395ca7d15262d to your computer and use it in GitHub Desktop.
Haskellでモンテカルロ積分
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 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