Find π by throwing a billion darts at a circle.
Estimate the value of π with a Monte Carlo simulation.
Draw a unit circle. Its area is π(1)2 = π.
Draw a square which barely contains the circle. Each side has length 2. Its area is 22 = 4.
Choose a random point uniformly distributed on the square. The probability it is inside the circle is π/4.
Choose 1 billion random points uniformly distributed on the square. If n of those points are in the circle, then π ≈ 1,000,000,000 n*.
Run make
to compile the program.
Estimate π. Requires interactive user input.
bin/picasino
g++
from the GNU Compiler Collection
> bin/picasino
How many million darts? 1000
Throwing darts...
Estimate: 3.1415884
Error: -4.2975898e-06
Tosses: 1000000000
Time: 18.545214 seconds
No.