Created
April 25, 2013 17:23
-
-
Save maddenpj/5461491 to your computer and use it in GitHub Desktop.
Monte carlo pi guessing machine
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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Only works for CIRCLE_RADIUS = 1