Skip to content

Instantly share code, notes, and snippets.

@tautologico
Created March 14, 2013 21:32
Show Gist options
  • Save tautologico/5165468 to your computer and use it in GitHub Desktop.
Save tautologico/5165468 to your computer and use it in GitHub Desktop.
Estimating pi by simulation of the Buffon's needle experiment. Vectorized version.
# 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