Created
December 19, 2014 15:06
-
-
Save timball/b49e32791066df55abb4 to your computer and use it in GitHub Desktop.
monte carlo pi
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
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