Write a program and estimate pi using 400, 4000, 40000, 4000

Consider a dartboard of radius 1. Since the area of the board will be  π r2 = π * 1 *1, it's clear the area of the dartboard is exactly π. The area of a square surrounding the board (circumscribing it) would be 2*2 = 4, since the diameter or the dartboard is 2 (twice the radius) and the diameter equals length of a side of the square. Now, if we introduce a set of darts, where the x and y range from -1 to +1, we simulate a random sampling of all positions on the square surrounding the dartboard, some on the board and some of the square surrounding it. You can determine whether the dart hit the board or not by simply calculating if its distance from the center of the dartboard is greater than 1 (off the board) or <=1 (on the board). If we toss 4 darts, corresponding to area of the square, we will expect π darts, on average, to hit inside the dartboard and the remainder to miss. By extension, if we toss 40000 darts, 10000*π  should hit the dartboard. We can use this largest set to give us a decent estimation.

 

   Related Questions in Programming Languages

©TutorsGlobe All rights reserved 2022-2023.