Skip to content

Instantly share code, notes, and snippets.

@pprzetacznik
Last active August 29, 2015 14:22
Show Gist options
  • Save pprzetacznik/2e54e03579de14798ace to your computer and use it in GitHub Desktop.
Save pprzetacznik/2e54e03579de14798ace to your computer and use it in GitHub Desktop.
from random import random
NUM_SAMPLES = 10000
def sample(p):
x, y = random(), random()
return 1 if x*x + y*y < 1 else 0
count = sc.parallelize(xrange(0, NUM_SAMPLES)).map(sample) \
.reduce(lambda a, b: a + b)
print "Pi is roughly %f" % (4.0 * count / NUM_SAMPLES)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment