-
-
Save danslimmon/3b26bdaf1668660b56ea to your computer and use it in GitHub Desktop.
Couldn't find an accurate estimate of π anywhere, so I decided to Monte Carlo it
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
#!/usr/bin/python | |
import random | |
N_REP = 9999999 | |
c = 0 | |
for i in range(N_REP): | |
x, y = (random.random()-.5, random.random()-.5) | |
if (x*x+y*y)**.5 < .5: | |
c += 1 | |
print("pi is approximately {0}".format(4.0 * float(c) / float(N_REP))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment