Created
July 2, 2013 07:35
-
-
Save rinx/5907399 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
-- if this program stack space overflow, | |
-- ./monte_carlo +RTS -K200M | |
import System.Random | |
import Control.Monad | |
main :: IO () | |
main = do | |
randsx <- replicateM s $ (randomRIO(0,1.0)::IO Double) | |
randsy <- replicateM s $ (randomRIO(0,1.0)::IO Double) | |
print (getPI (isInTheCircle randsx randsy) sDouble) | |
where | |
s = 100000 | |
sDouble = realToFrac s | |
isInTheCircle :: [Double] -> [Double] -> Double | |
isInTheCircle [] _ = 0 | |
isInTheCircle _ [] = 0 | |
isInTheCircle (x:xs) (y:ys) | |
| l <= 1 = 1 + isInTheCircle xs ys | |
| l > 1 = isInTheCircle xs ys | |
where l = x^2 + y^2 | |
getPI:: Double -> Double -> Double | |
getPI x s = x * 4 / s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment