Skip to content

Instantly share code, notes, and snippets.

@rinx
Created July 2, 2013 07:35
Show Gist options
  • Save rinx/5907399 to your computer and use it in GitHub Desktop.
Save rinx/5907399 to your computer and use it in GitHub Desktop.
-- 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