Monte Carlo methods: Difference between revisions

m
Line 1,297:
 
<lang haskell>import Control.Monad (foldM, (>=>))
import Data.Functor ((<&>))
import System.Random (randomRIO)
 
Line 1,304 ⟶ 1,305:
monteCarloPi n =
foldM go 0 [1 .. n]
<&>>= (pure . (/ fromIntegral n) . (4 *) . fromIntegral)
where
rnd = randomRIO (0, 1) :: IO Double
go a _ = rnd >>= (\rx -> rnd <&> flip (f rx) a)
rnd >>= (\rx -> rnd >>= (pure . flip (f rx) a))
f x y
| 1 > x ** 2 + y ** 2 = succ
9,655

edits