Created
November 1, 2016 06:24
-
-
Save christiangenco/7e544888f232a60a334847d3fd9ad77f to your computer and use it in GitHub Desktop.
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
def pi_monte(n=100_000) | |
inside = total = 0 | |
n.times{ | |
x, y = rand*2-1, rand*2-1 | |
d = Math.sqrt(x**2+y**2) | |
if d < 1 | |
inside += 1 | |
end | |
total += 1 | |
} | |
(1.0*inside/total)*4 | |
end | |
pi = pi_monte | |
p pi | |
p ((pi - Math::PI) / Math::PI) * 100 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment