Monte Carlo methods: Difference between revisions
Content added Content deleted
m (→{{header|Go}}: add variant with x/exp/rand) |
(Added an example in Erlang) |
||
Line 577: | Line 577: | ||
10000000 samples: PI = 3.141124 |
10000000 samples: PI = 3.141124 |
||
</pre> |
</pre> |
||
=={{header|Erlang}}== |
|||
<lang ERLANG> |
|||
-module(monte). |
|||
-export([main/1]). |
|||
monte(N)-> |
|||
monte(N,0,0). |
|||
monte(0,InCircle,NumPoints) -> |
|||
4 * InCircle / NumPoints; |
|||
monte(N,InCircle,NumPoints)-> |
|||
Xcoord = rand:uniform(), |
|||
Ycoord = rand:uniform(), |
|||
monte(N-1,if Xcoord*Xcoord + Ycoord*Ycoord < 1 -> InCircle + 1; true -> InCircle end, NumPoints + 1). |
|||
main(N) -> io:format("PI: ~w~n", [ monte(N) ]). |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
8> [monte:main(X) || X <- [10000,100000,100000,10000000] ]. |
|||
PI: 3.136 |
|||
PI: 3.1464 |
|||
PI: 3.1412 |
|||
PI: 3.1416704 |
|||
[ok,ok,ok,ok] |
|||
</pre> |
|||
=={{header|ERRE}}== |
=={{header|ERRE}}== |