Skip to content

Instantly share code, notes, and snippets.

@christiangenco
Created November 1, 2016 06:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christiangenco/7e544888f232a60a334847d3fd9ad77f to your computer and use it in GitHub Desktop.
Save christiangenco/7e544888f232a60a334847d3fd9ad77f to your computer and use it in GitHub Desktop.
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