Skip to content

Instantly share code, notes, and snippets.

@maddenpj
Created April 25, 2013 17:23
Show Gist options
  • Save maddenpj/5461491 to your computer and use it in GitHub Desktop.
Save maddenpj/5461491 to your computer and use it in GitHub Desktop.
Monte carlo pi guessing machine
import random
import sys
CIRCLE_RADIUS = 1
NUM_GUESSES = int(sys.argv[1])
def insideCircle(px, py):
l = px**2 + py**2
return (l <= CIRCLE_RADIUS)
inCircle = 0
for i in xrange(NUM_GUESSES):
x = random.random()
y = random.random()
if(insideCircle(x,y)):
inCircle += 1
print (inCircle/float(NUM_GUESSES))*4
@maddenpj
Copy link
Author

Only works for CIRCLE_RADIUS = 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment