1. Monte Carlo π

The following code uses a Monte Carlo algorithm to estimate π:

// Use Monte Carlo simulation to estimate pi.
private double MonteCarloPi(long numPoints)
{
Random rand = new Random();

// Make a bitmap to show points.
int wid = pointsPictureBox.ClientSize.Width;
int hgt = pointsPictureBox.ClientSize.Height;
Bitmap bm = new Bitmap(wid, hgt);
using (Graphics gr = Graphics.FromImage(bm))
{
gr.Clear(Color.White);
gr.DrawEllipse(Pens.Black, 0, 0, wid - 1, hgt - 1);
}

// Make the random points.
int numHits = 0;
for (int i = 0; i < numPoints; i++)
{
// Make a random point 0 <= x < 1.
double x = rand.NextDouble();
double y = rand.NextDouble();

// See how far the point is from (0.5, 0.5).
double dx = x - 0.5;
double dy = y - 0.5;
if (dx * dx + dy * dy < 0.25) numHits++;

// Plots up to 10,000 points.
if (i < 10000)
{
int ix = (int)(wid * x);
int iy = (int)(hgt * y);
if (dx * dx + dy * dy < 0.25)
bm.SetPixel(ix, iy, Color.Gray);
else
bm.SetPixel(ix, iy, Color.Black);
}
}

// Display the plotted points.
pointsPictureBox.Image = bm;

    // Get the hit fraction.
double fraction = numHits / (double)numPoints;

// Estimate pi.
return 4.0 * fraction;
}

The method starts by creating a Random object that it can use to generate random numbers. It then creates a bitmap to fit the program's PictureBox, associates a Graphics object with it, clears the bitmap, and draws a circle centered in the bitmap.

Next, the code uses a loop to generate the desired number of random points within the square 0 ≤ X, Y ≤ 1. The NextDouble method of the Random class returns a value between 0 and 1, so generating the point's X and Y coordinates is relatively easy.

The code then determines whether the point lies within the circle that fills the square 0 ≤ X, Y ≤ 1. To do that, the method calculates the distance from the random point to the center of the circle (0.5, 0.5). It then determines whether that distance is less than the circle's radius.

Actually, the code doesn't really find the distance between the point and (0.5, 0.5). To do that, it would use the distance formula to find the distance and then use the following equation to determine whether the result is less than the circle's radius 0.5:

Calculating square roots is relatively slow, however, so the program squares both sides of the equation and uses the following equation instead:

The value 0.5 squared is 0.25, so the program actually tests whether:

The program then plots the point on the bitmap in either gray or black, depending on whether the point lies within the circle. The code also uses the numHits variable to keep track of the number of points that lie within the circle.

After it finishes generating points, the code makes its approximation for π. The square 0 ≤ X, Y ≤ 1 has an area of 1.0 and the circle should have the area π × R2 where R is the circle's radius. In this example, R is 0.5, so the fraction of points that fall inside the circle should be the following:

If you solve this equation for π, you get the following:

The code gets the fraction of the points that fell within the circle, multiples that by 4.0, and returns the result as its estimate for π.

The following screenshot shows the MonteCarloPi example solution approximating π. After generating 10,000 random points, its approximation for π is off by around 1%. Using more points produces better approximations for π. The result with million points is correct within about 0.1–0.2%, and the result with 100 million points is correct to within around 0.01%:

Download the MonteCarloPi example solution to see additional details.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.77.76