n: integer, number of Monte Carlo samples, defaults to 1000
Details
A crude Monte Carlo estimate of pi can be formed as follows. Sample from the unit square many times (i.e., each sample is formed with two independent draws from a uniform density on the unit interval). Compute the proportion p of sampled points that lie inside a unit circle centered on the origin; such points (x,y) have distance from the origin d=sqrt(x2+y2) less than 1. Four times p is a Monte Carlo estimate of pi. This function is a wrapper to a simple C function, bringing noticeable speed gains and memory efficiencies over implementations in native R.
Contrast this Monte Carlo method with Buffon's needle and refinements thereof (see the discussion in Ripley (1987, 193ff).
Returns
the Monte Carlo estimate of pi
References
Ripley, Brain D. 1987 [2006]. Stochastic Simulation. Wiley: Hoboken, New Jersey.