21 lines
507 B
Text
21 lines
507 B
Text
program monte_carlo;
|
|
setrandom(0);
|
|
|
|
loop for x in [5..8] do
|
|
throws := 10**x;
|
|
print(throws, " => ", calc_pi(throws));
|
|
end loop;
|
|
|
|
proc calc_pi(throws);
|
|
inside := 0;
|
|
loop init i := 0; while i<throws step i +:= 1; do
|
|
x := random 1.0;
|
|
y := random 1.0;
|
|
if x*x + y*y <= 1 then
|
|
inside +:= 1;
|
|
end if;
|
|
throws -:= 1;
|
|
end loop;
|
|
return 4*inside/throws;
|
|
end proc;
|
|
end program;
|