monte carlo simulation if you try buffon' experimentyou'll soon realize that the places where the needles land are not truly random moreovereven if you could drop them randomlyit would take very large number of needles to get an approximation of as good as even the bible' fortunatelycomputers can randomly drop simulated needles at ferocious rate figure contains program that estimates using the buffon-laplace method for simplicityit considers only those needles that fall in the upper right-hand quadrant of the square the function throwneedles simulates dropping needle by first using random random to get pair of positive cartesian coordinates ( and valuesit then uses the pythagorean theorem to compute the hypotenuse of the right triangle with base and height this is the distance of the tip of the needle from the origin (the center of the squaresince the radius of the circle is we know that the needle lies within the circle if and only if the distance from the origin is no greater than we use this fact to count the number of needles in the circle the function getest uses throwneedles to find an estimate of by dropping numneedles needles and averaging the result over numtrials trials the function estpi calls getest with an ever-growing number of needles until getest returns an estimate thatwith confidence of %is within precision of the actual value it does this by calling throwneedles with an ever-larger number of needlesuntil the standard deviation of the results of numtrials trials is no larger than precision/ under the assumption that the errors are normally distributedthis ensures that of the values lie within precision of the mean