      Using Buffon's Needles for Estimating the Value of Pi


                               by

                          Earl F. Glynn
                        Overland Park, KS
                     Compuserve [73257,3527]



           Instructions for Running the BUFFON Program

Run this program by entering BUFFON and optionally, a numeric
parameter.  The parameter defines how many needle throws are made
before clearing and rescaling the view.  The default value is
2000.  Use a smaller number to keep more black space on the screen.
An invalid parameter is ignored.

The BUFFON program automatically detects the highest-resolution
video mode available, CGA, EGA or VGA.  A way of overriding this
autodetection with DOS environment variables is described in the
source program header comments and is shown in the DEMO.BAT file.

BUFFON continuously throws needles and periodically changes the
scaling parameters to add variety to the display.  Once the program
is interrupted by pressing any key, an exit summary displays
various information, including "hits", elapsed time, and an
approximate value of pi.



                   BUFFON Program Description

A Monte Carlo method [1] for calculating the numeric value of pi
() involves randomly throwing short line segments (e.g., needles,
toothpicks, pencils, etc.) onto a plane of equally-spaced parallel
lines (e.g., brick sidewalks, hardwood floors, certain tiles,
etc.).  The number of segments crossing the parallel lines is
related to the value .  Count Buffon first experimented with this
method in the 1700s when he dropped needles onto parallel lines [1,
2].

"Dropping" Buffon's needles onto a graphical display provides a
screensaver program that reports a value of  after it is
interrupted.  But don't expect a lot of precision from this method.
The BUFFON program yields a value of approximately 3.1 for  by
using pseudo-random numbers to "throw" the needles.

The algorithm [see below] describes the mechanics necessary to
"throw" Buffon's needles onto a computer display (but does not
address the conversion from "world coordinates" to "screen
coordinates").  An appendix explains the mathematical details of
how throwing needles onto parallel lines results in an estimate of
pi.


                           References

[1] Anthony Ralston (editor), Encyclopedia of Computer Science and
          Engineering (second edition), New York:  Van Nostrand
          Reinhold, 1983, pp. 997-998.

[2] Richard W. Hamming, Introduction to Applied Numerical Analysis,
          New York:  McGraw Hill, 1971, pp. 311-312.




                             Algorithm

     Assumptions:
          1.   xMin, xMax, yMin, yMax define a rectangular
               throwing area:

               yMax Ŀ
                                               
                    Ĵ
                    .                           .
                    Ĵ
                    .                           .
                    Ĵ
               yMin 
                   xMin                        xMax

          2.  unit spacing between horizontal parallel lines
          3.  needle length, L  1

     toss <-- 0
     crossing <-- 0
     REPEAT

       define needle's midpoint distance from parallel line:
        <-- RANDOM[-0.5,0.5]

       define direction vector (x,y) to assign needle orientation:
       x <-- RANDOM[-1.0,1.0]
       y <-- RANDOM[ 0.0,1.0]
       r <-- SQRT[x^2 + y^2]

       sin <-- y/r
       yDelta <-- Lsin

       cos <-- x/r
       xDelta <-- Lcos

       define midpoint of needle in throwing space:
       xMid <-- RANDOM[xMin,xMax]
       yMid <--  + TRUNCATE[ RANDOM[yMin,yMax] ]

       plot the needle:
       PLOTLINE FROM (xMid-xDelta,yMid-YDelta) TO
                     (xMid+xDelta,yMid+yDelta)

       update statistics:
       count the toss
       IF   ABS()  yDelta
       THEN count the crossing

     UNTIL KeyPressed

       2toss/crossing




                       Mathematical Details

Consider needles (or toothpicks, pencils, etc.) of length L being
thrown randomly onto a plane of parallel lines, which are a fixed
1 unit apart (see Figure 1).


              
            
            
                                       /"Hit"
          1 unit                       /
                                  L /
                                    /
                        (xmid,ymid).     
                                  /    
              īL/   Lsin 
                                 /       
                                /      
                                
                               Lcos

                                    "Miss"
                                         L  1

               

               Figure 1.  "Hit" and "Miss" Tosses.



Let the midpoint of each needle be (xmid,ymid).  With L  1, the
distance, , from this midpoint to the nearest parallel line ranges
from 0 to .

Let  be the angle formed by the needle and the x-axis.  This angle
ranges from 0 to .

The needle crosses (or touches) a parallel line when

                              Lsin                       (1)

Considering all possible values of  and , Figure 2 shows the area
under the curve Lsin from 0 to , which is the condition for a
needle to cross one of the parallel lines:



                     Ŀ
                                               
                  L      . -.     
                          .           .       
                        .   Lsin       .    
                      .                      . 
                    0 
                      0                       

                                   

              Figure 2.  The needle crosses a line
           for values  and  under the curve Lsin.



The probability of a needle crossing a parallel line is the ratio
of the area under the curve to the area of the whole box, which can
be evaluated analytically as:

                               area under curve
   probability of crossing  =                 (2)
                                 area of box


                                
                                
                                   L sin d
                                0                 2 x L
                            =   =        (3)
                                                  


This probability is also the ratio of line crossings to tosses:

                               # crossings
   probability of crossing  =                      (4)
                                # tosses



                                    2 x L x # tosses
Equating (3) and (4) gives                  (5)
                                      # crossings
