Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.