[Haskell-cafe] approximating pi
dons at galois.com
Sun Apr 27 20:35:23 EDT 2008
> By picking points randomly from a square one can calculate pi. Keep
> track of how many points from the square you pick lay in the inscribed
> circle. Supposing the square's edges are length 2, then the inscribed
> circle has radius 1. Thus the area of the circle is pi*r^2 = pi. The
> area of the square is 4. The ratio of points picked from the circle
> to the total number of picked points will converge to pi / 4.
> What is the best way to express this algorithm in Haskell?
Using a random generator, such as System.Random or
System.Random.Mersenne, generate random numbers in your range,
testing if they're inside the circle, and loop until your limit
condition is reached.
The full program is about 10 lines or so, so shouldn't be too hard to
work out, once you've worked out how to generate Doubles from
More information about the Haskell-Cafe