Created
March 14, 2013 21:32
-
-
Save tautologico/5165468 to your computer and use it in GitHub Desktop.
Estimating pi by simulation of the Buffon's needle experiment. Vectorized 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 the Buffon's needle experiment | |
function mc_pi(repeats::Int) | |
halfpi = asin(1) | |
x = rand(repeats) | |
theta = halfpi * rand(repeats) | |
crosses = sum( x .<= 0.5 * sin(theta) ) | |
repeats / crosses | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment