Skip to content

Instantly share code, notes, and snippets.

@timball
Created December 19, 2014 15:06
Show Gist options
  • Save timball/b49e32791066df55abb4 to your computer and use it in GitHub Desktop.
Save timball/b49e32791066df55abb4 to your computer and use it in GitHub Desktop.
monte carlo pi
from random import uniform
def direct_pi(N):
n_hits = 0
for i in range(N):
x, y = uniform(-1.0, 1.0), uniform(-1.0, 1.0)
if x ** 2 + y ** 2 < 1.0:
n_hits += 1
return n_hits
n_trials = 100000
for attempt in range(10):
print attempt, 4 * direct_pi(n_trials) / float(n_trials)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment