[Tutor] re: Monte Python method you suggested for pi

Charles Gruschow, Jr. c.gruschow@prodigy.net
Mon, 28 Aug 2000 00:45:07 -0500


program I figured out is below:

I used the real.py module from:
##real.py was obtained from
ftp://ftp.python.org/pub/www.python.org/ftp/python/contrib-09-Dec-1999/DataS
tructures/real-accurate.pyar
##from the Vaults of Parnassus website

(using real.py module:
pi(1000)=3.1415926535897932384626433832795028841971693
993751058209749445923078164062862089986280348253
421170679821480865132823066470938446095505822317
253594081284811174502841027019385211055596446229
4895493038196442881097566593344612847564823378678
31652712019091456485669234603486104543266482133936
072602491412737245870066063155881748815209209628292
54091715364367892590360011330530548820466521384146951
941511609433057270365759591953092186117381932611793105
1185480744623799627495673518857527248912279381830119491
298336733624406566430860213949463952247371907021798609437
027705392171762931767523846748184676694051320005681271452635
6082778577134275778960917363717872146844090122495343014654958537
10507922796892589235420199561121290219608640344181598136297747713
0996051870721134999999837297804995105973173281609631859502445945534
690830264252230825334468503526193118817101000313783875288658753320838
14206171776691473035982534904287554687311595628638823537875937519577818
5778053217122680661300192787661119590921642019894+-2
(the +-2 is error n last digit)(pi to a thousand decimal places))

Did you mean something like this:

##There's a very very cool way of calculating Pi using a "Monty Carlo"
##method.  That is, we can get Pi's value through random numbers!  I haven't
##tried it out myself yet, but let me see if I can remember it... Here's the
##general idea --- let's call it the dart throwing method:
##
##
##Ok, so you know that a circle's area is (Pi * r**2).  Draw a circle of
##radius 1:
##
##     ***
##    *   *
##        1
##
##Our center.  Ok, now draw a square that surrounds that circle.  The square
##itself has sides of length 2.
##
##      2
##   -------
##   | *** |
##   |*   *|
##   |*   *|
##   | *** |
##   -------
##      |--|
##        1
##
##Our dartboard.  We can see that our picture can be divided into two
##sections --- the region inside the circle, and the region outside of the
##circle.
##
##Now imagine sampling random dots on this picture.  You're throwing darts.
##Some of the darts will be in the circle, while others will fall outside
##the circle.  You can write a program that simulates dart throwing, and can
##count how many go inside or outside the circle.
##
##
##Probablistically, the ratio between those two counts should approximate
##the ratio between the _areas_ of inside and outside.  That is,
##
##area of inside/area of outside ==
##    (number of points in circle)/(number of points outside circle)
##
##But we already know that the area inside our circle == Pi, and the area of
##the outside is (area of square - circle) == 4 - Pi.
##
##
##There might be an easier way to simplify the algebra, or a simpler way of
##looking at this problem, so you might want to play around with it for a
##while.
##
##
##    *   *
##     ***
##      |--|
##        1
##
##Our center.  Ok, now draw a square that surrounds that circle.  The square
##itself has sides of length 2.
##
##      2
##   -------
##   | *** |
##   |*   *|
##   |*   *|
##   | *** |
##   -------
##      |--|
##        1
##
##Our dartboard.  We can see that our picture can be divided into two
##sections --- the region inside the circle, and the region outside of the
##circle.
##
##Now imagine sampling random dots on this picture.  You're throwing darts.
##Some of the darts will be in the circle, while others will fall outside
##the circle.  You can write a program that simulates dart throwing, and can
##count how many go inside or outside the circle.
##
##
##Probablistically, the ratio between those two counts should approximate
##the ratio between the _areas_ of inside and outside.  That is,
##
##area of inside/area of outside ==
##    (number of points in circle)/(number of points outside circle)
##
##But we already know that the area inside our circle == Pi, and the area of
##the outside is (area of square - circle) == 4 - Pi.
##
##
##There might be an easier way to simplify the algebra, or a simpler way of
##looking at this problem, so you might want to play around with it for a
##while.
##
##
##I hope this gives you enough to make a Pi finding program.  You might need
##to throw a lot of darts to get accuracy out of this thing, but you should
##get a reasonable approximation this way.  The whrandom module, which has
##functions to get random numbers, should be really helpful with this.
##
##area in=pi, area
out=4-pi,pi/(4-pi)=ratio,ratio*(4-pi)=pi,4*ratio-pi*ratio=pi
##4*ratio=pi+pi*ratio,4*ratio=pi(1+ratio),(4*ratio)/(1+ratio)=pi
##3.659792366325487694478707269257069621324770947309660129799649133113166238
050709740338287247078949605+-1
##would be ideal ratio to 100 decimal places
##real.py was obtained from
ftp://ftp.python.org/pub/www.python.org/ftp/python/contrib-09-Dec-1999/DataS
tructures/real-accurate.pyar
##from the Vaults of Parnassus website


import whrandom
from whrandom import *
import real
from real import *

seed(x=0,y=0,z=0)

ptin,ptout=0,0

for a in range(1,1000):
    seed(x=0,y=0,z=0)
    xt,yt=uniform(-1.0,1.0),uniform(-1.0,1.0)
    rsqr,r=xt**2+yt**2,sqrt(rsqr)
    if rsqr==1:
       #print xt,yt,r,"on circle"
        ptin=ptin+1
    if rsqr<1:
        #print xt,yt,r,ptin,"inside circle"
        ptin=ptin+1
    if rsqr>1:
        #print xt,yt,r,ptout,"outside circle"
        ptout=ptout+1
    if ptout>0:
        print a,ptin,ptout,float(ptin)/float(ptout)

fptin=float(ptin)
fptout=float(ptout)
ptrat=fptin/fptout
print fptin,fptout,ptrat,(4*ptrat)/(1+ptrat)





----- Original Message -----
From: Daniel Yoo <dyoo@hkn.EECS.Berkeley.EDU>
To: Charles Gruschow, Jr. <c.gruschow@prodigy.net>
Cc: <tutor@python.org>
Sent: Sunday, August 27, 2000 4:47 AM
Subject: Re: [Tutor] can python calculate the digits of pi or e?,has
thisbeen done before?,how?


> On Fri, 25 Aug 2000, Charles Gruschow, Jr. wrote:
>
> > can python calculate the digits of pi or e?,has this been done
> > before?,how?
>
>
> There's a very very cool way of calculating Pi using a "Monty Carlo"
> method.  That is, we can get Pi's value through random numbers!  I haven't
> tried it out myself yet, but let me see if I can remember it... Here's the
> general idea --- let's call it the dart throwing method:
>
>
> Ok, so you know that a circle's area is (Pi * r**2).  Draw a circle of
> radius 1:
>
>      ***
>     *   *
>     *   *
>      ***
>       |--|
>         1
>
> Our center.  Ok, now draw a square that surrounds that circle.  The square
> itself has sides of length 2.
>
>       2
>    -------
>    | *** |
>    |*   *|
>    |*   *|
>    | *** |
>    -------
>       |--|
>         1
>
> Our dartboard.  We can see that our picture can be divided into two
> sections --- the region inside the circle, and the region outside of the
> circle.
>
> Now imagine sampling random dots on this picture.  You're throwing darts.
> Some of the darts will be in the circle, while others will fall outside
> the circle.  You can write a program that simulates dart throwing, and can
> count how many go inside or outside the circle.
>
>
> Probablistically, the ratio between those two counts should approximate
> the ratio between the _areas_ of inside and outside.  That is,
>
> area of inside/area of outside ==
>     (number of points in circle)/(number of points outside circle)
>
> But we already know that the area inside our circle == Pi, and the area of
> the outside is (area of square - circle) == 4 - Pi.
>
>
> There might be an easier way to simplify the algebra, or a simpler way of
> looking at this problem, so you might want to play around with it for a
> while.
>
>
> I hope this gives you enough to make a Pi finding program.  You might need
> to throw a lot of darts to get accuracy out of this thing, but you should
> get a reasonable approximation this way.  The whrandom module, which has
> functions to get random numbers, should be really helpful with this.
> Here's the link to it:
>
>    http://python.org/doc/current/lib/module-whrandom.html
>
> If you need more help with this method, email again to tutor@python.org.
> Good luck!
>