Created
March 14, 2013 21:38
-
-
Save tautologico/5165499 to your computer and use it in GitHub Desktop.
Estimate pi by simulation of Buffon's needle. Iterative version.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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