Last active
May 24, 2020 01:49
-
-
Save Fryie/10376ca79f3551e0258f2a6e7b26a7fc to your computer and use it in GitHub Desktop.
Estimating pi by the monte carlo method
This file contains 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
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