Skip to content

Instantly share code, notes, and snippets.

@FavioVazquez
Created April 17, 2018 15:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save FavioVazquez/d500f92732dca3793ce862d7457a6ed8 to your computer and use it in GitHub Desktop.
Save FavioVazquez/d500f92732dca3793ce862d7457a6ed8 to your computer and use it in GitHub Desktop.
import findspark
findspark.init("/opt/spark")
import random
from pyspark import SparkContext
sc = SparkContext(appName="EstimatePi")
def inside(p):
x, y = random.random(), random.random()
return x*x + y*y < 1
NUM_SAMPLES = 1000000
count = sc.parallelize(range(0, NUM_SAMPLES)) \
.filter(inside).count()
print("Pi is roughly %f" % (4.0 * count / NUM_SAMPLES))
sc.stop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment