Jump to content

Monte Carlo methods: Difference between revisions

m
(→‎{{header|Haskell}}: Applied Ormolu to older version, and replaced its 'foldl (+) 0' with sum)
Line 1,300:
 
monteCarloPi :: Int -> IO Double
monteCarloPi n = do
m <- foldM go 0 [1 .. n]
return (4 *>>= fromIntegral(pure m. (/ fromIntegral n) . (4 *) . fromIntegral)
where
rnd = randomRIO (0, 1) :: IO Double
Line 1,308:
| 1 > x ** 2 + y ** 2 = succ
| otherwise = id
 
go a x = do
go a rxx <- rnd=
ryrnd >>= (\rx <-> rnd >>= (pure . flip (f rx) a))
return $ f rx ry a
 
main :: IO ()
9,659

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.