Solutions
The following sections describe solutions to the preceding problems. You can download the example solutions to see additional details and to experiment with the programs at https://github.com/PacktPublishing/Improving-your-C-Sharp-Skills/tree/master/Chapter15.
Â
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...