Skip to content

Instantly share code, notes, and snippets.

@AdroitAnandAI
Last active June 6, 2021 05:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AdroitAnandAI/0ba9e3b0b5e505060941e540d2707374 to your computer and use it in GitHub Desktop.
Save AdroitAnandAI/0ba9e3b0b5e505060941e540d2707374 to your computer and use it in GitHub Desktop.
To compute pi value
# Compute value of PI using Monte Carlo Approach
# Source: http://spark.apache.org/examples.html
NUM_SAMPLES = 1000000
def inside(p):
x, y = random.random(), random.random()
return x*x + y*y < 1
# filter: http://spark.apache.org/docs/latest/api/python/pyspark.html
count = sc.parallelize(range(0, NUM_SAMPLES),10) \
.filter(inside).count()
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