Skip to content

Instantly share code, notes, and snippets.

@Fryie
Last active May 24, 2020 01:49
Show Gist options
  • Save Fryie/10376ca79f3551e0258f2a6e7b26a7fc to your computer and use it in GitHub Desktop.
Save Fryie/10376ca79f3551e0258f2a6e7b26a7fc to your computer and use it in GitHub Desktop.
Estimating pi by the monte carlo method
using Distributions
function estimate_pi(n) # the higher n is, the more accurate the estimate will be
hits = 0
for i=1:n
x = rand(Uniform(-1,1))
y = rand(Uniform(-1,1))
if x*x + y*y <= 1
hits += 1
end
end
4*(hits/n)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment