Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: Pi calculator

by thaigrrl (Monk)
on Feb 15, 2001 at 23:19 UTC ( #58694=note: print w/replies, xml ) Need Help??

in reply to Pi calculator

I found this to be very cool. I wrote a paper on AI and the Monte Carlo Method in college so I found it even more interesting. Not a very efficient method of calculating PI but really trivial because of the accuracy of PI as the cycles increase. Did you come up with this on your own? Is this based on the Buffon pin drop method, where he drops a pin of length X on a page of parallel lines? If so bravo I found it really entertaining :)
Also the use of srand and rand (forcing the random number to be less than 1 but greater than zero) is pretty sweet. I wouldn't have thought of that.

Buffon's Needle Experiment:
If we have a uniform grid of parallel lines, unit distance apart and if we drop a needle of length k < 1 on the grid, the probability that the needle falls across a line is 2k/pi. Various people have tried to calculate by throwing needles. The most remarkable result was that of Lazzerini (1901), who made 34080 tosses and got

PI = 355/113 = 3.1415929


New comment:
on Thurs Feb 15 2001 at 16:40 (EST)
I actually did some testing on this, and it is pretty random, I plugged in a bunch of numbers, and got 3.14... with the cycle 754854. I retested the numbers and I got some results:

$yespi / cycle * 4 = PI 593360 / 754854 * 4 = is 3.14423716374292 593195 / 754854 * 4 = is 3.14336282247958 592995 / 754854 * 4 = is 3.14230301488765

The problem here is not srand. Think of an x,y plane with a 1x1 square with its center located at (0,0). Inside this square is a circle with a radius of 1, also with its center at (0,0) what is happening in this algorithm is you are coming up with two random numbers but only couting your $yespi if (x^2 + y^2) <=1 . This could be any coordinate inside the square, therefore all of the points outside the circle but inside the square will not equal pi. But everything inside the circle will. You get a number close to PI more often than not because the area within the circle is greater than that outside of the circle but within the square. Does this make sense?

Replies are listed 'Best First'.
Re: Re: Pi calculator
by Fingo (Monk) on Feb 16, 2001 at 01:24 UTC
    Thank you. I did not come up with the method if that's what you are asking. Its based on a circle with a radius of 1 and a square dartboard with each sid being 2. The circle is oin the square. WHen you throw the dart, there is a propability of pi/4 that it will land.... Can you please help me with something? I modified the code to repeat the calculations a certain number of times adding 10 cycles each time. As it got farther than 50,000 it started going into 3.16... while it wa at 3.14 in the 50,000 area. Is this a nuence of the method, or is this have to do with srand not being completly random? Also after 50,000 or so, not once did it generate pi starting with 3.14 ?!?
      Running for 10,000,000 twice I got 3.1405 the first run, and 3.1424 the second. (for an average of 3.1415) It's quite likely your rand() isn't perfect. (neither is mine 10,000,000 runs should give me a digit or two more accuracy).

      Anyway, here is my favorite approximation for pi, mainly because it only uses the number 2. Even though two is normally a computer friendly number, this algorithm isn't, because it also uses sqrt's. With my perl, 14 iterations is gives the maximum accuracy: 3.14159265480759

      #!/usr/bin/perl -w use strict; print "Enter how many iterations:\n"; chomp(my $i = <>); my $x = $i - 1; my $y = sqrt(2); do { $y = sqrt(2 + $y) while (--$x); $y = sqrt(2 - $y); } if $x; my $z = $y * (2 ** $i); print "Pi is close to: $z\n";

      So, while not the best, but I have some strange affinity to it. :)


      p.s. Sorry for the cryptic code for a quick decrypt its: (2**n)*sqrt(2-sqrt(2+sqrt(2+sqrt(2+sqrt(2))))) with the number of 2's inside the sqrt equaling n.

        #!/usr/bin/perl # This is a quick program to calculate pi using the Monte Carlo method +. # I recomend inputting a value for $cycles greater that 1000. # I am working on a detailed explanation of how and why this works. # I will add it as soon as I'm done. use strict; open(PI, ">>pi.dat") || die "pi.dat"; my ($i, $j, $yespi, $pi) = 1; my $cycles = 1; srand; while ($j <= 100000) { $cycles = $j; while ($i <= $cycles) { my ($x, $y, $cdnt) = 1; $x = rand; $y = rand; $cdnt = $x**2 + $y**2; if ($cdnt <= 1) { ++$yespi; } $i=$i + 10; $pi = ($yespi / ($cycles / 10)) * 4; # since I add 10 every time. print PI "$cycles $pi\n"; } $j = $j + 10; } close(PI) || die "pi.dat";
        Here is the modified script I was talking about. If you look at pi.dat it starts getting inacurate at one point. I will try using one of the ways to genereate seeds that are slightly more random than plain srand. Note: you need to make a file pi.dat for the script to run.
Re: Re: Pi calculator
by Anonymous Monk on Feb 17, 2001 at 01:12 UTC
    Thaigrrrr Maybe I did not quite understand you. Thats exactly how the algorithm is supposed to work. The circle has a radius of 1, so it's area is pi (A=pi*r**2) and the square has an area of 4 since each side is 2. So the propability of any coordinate landing in the circle becomes area of circle/area of square or pi/4 so we divide the number that landed in the circle by the number overal and multiply the result by 4. Is this what you meant?
      Oops forgot to log in, above post was mine
Buffon's Needle Link?
by gryng (Hermit) on Feb 16, 2001 at 06:19 UTC
    Could you provide a link? I'm too lazy to search, but I would still like to read more about this particular approach.

    Supposedly there is a modern pi generating algorithm which takes constant time to generate any given digit of pi requested.


      The former is available from Google, laziness is no excuse. The latter only works for base 16.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://58694]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2022-08-13 10:07 GMT
Find Nodes?
    Voting Booth?

    No recent polls found