Skip to content

Instantly share code, notes, and snippets.

@tautologico
Created March 14, 2013 21:38
Show Gist options
  • Save tautologico/5165499 to your computer and use it in GitHub Desktop.
Save tautologico/5165499 to your computer and use it in GitHub Desktop.
Estimate pi by simulation of Buffon's needle. Iterative version.
# Estimate pi simulating Buffon's needle experiment
function mc_pi_iter(repeats::Int)
halfpi = asin(1)
crosses = 0
for i in 1:repeats
x = rand()
theta = halfpi * rand()
if (x <= 0.5 * sin(theta))
crosses = crosses + 1
end
end
repeats / crosses
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment