RosettaCodeData/Task/Monte-Carlo-methods/Erlang/monte-carlo-methods-2.erl
2023-07-01 13:44:08 -04:00

20 lines
401 B
Erlang

-module(monte2).
-export([main/1]).
monte(N)->
monte(N,0,0).
monte(0,InCircle,NumPoints) ->
4 * InCircle / NumPoints;
monte(N,InCircle,NumPoints)->
X = rand:uniform(),
Y = rand:uniform(),
monte(N-1, within(X,Y,InCircle), NumPoints + 1).
within(X,Y,IN)->
if X*X + Y*Y < 1 -> IN + 1;
true -> IN
end.
main(N) -> io:format("PI: ~w~n", [ monte(N) ]).