Skip to content

Instantly share code, notes, and snippets.

@danslimmon
Created January 5, 2016 22:44
Show Gist options
  • Save danslimmon/3b26bdaf1668660b56ea to your computer and use it in GitHub Desktop.
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
#!/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