Created
March 10, 2011 01:25
-
-
Save ramalho/863393 to your computer and use it in GitHub Desktop.
improved version, using a complex number to represent a point
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
#!/usr/bin/env python | |
import sys | |
from random import uniform | |
TOTAL_DEFAULT = 10**6 | |
total = int(sys.argv[1]) if len(sys.argv) == 2 else TOTAL_DEFAULT | |
dentro = 0 | |
for i in xrange(total): | |
p = complex(uniform(-1, 1), uniform(-1, 1)) | |
if abs(p) <= 1: | |
dentro += 1 | |
print 'total:', total | |
print 'dentro:', dentro | |
print 'pi:', float(dentro)/total*4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment