Question: Simple code for MonteCarlo approximation of Pi

Hello everybody,

 

I find a answer on that subject at this link: http://www.mapleprimes.com/maplesoftblog/95508-Monte-Carlo-Pi

But for starting easy, I try this:

with(RandomTools[MersenneTwister]);
directpi := proc (n)
local i, x, y, N;
N := 0;
Digits := 2;
for i to n do
x := GenerateFloat();
y := GenerateFloat();
if x^2+y^2 < 1 then
N := N+1:
end if:
end do:
N;
end proc;


evalf(directpi(4000)/(1000.0), 4);

                             3.176

I would like to know if this code is appropriate and if it is possible to have a random number between -1 to 1 so it will be easier to plot the points in the square with the circle inside.

 

Thank you.

--------------------------------------
Mario Lemelin
Maple 18 Ubuntu 13.10 - 64 bits
Maple 18 Win 7 - 64 bits messagerie : mario.lemelin@cgocable.ca téléphone :  (819) 376-0987
Please Wait...