Skip to content

Instantly share code, notes, and snippets.

@danielrmeyer
Last active April 22, 2018 23:32
Show Gist options
  • Save danielrmeyer/22ea82aaa958b69295e779e3097cc7aa to your computer and use it in GitHub Desktop.
Save danielrmeyer/22ea82aaa958b69295e779e3097cc7aa to your computer and use it in GitHub Desktop.
Stochastic estimation of pi using dask
import dask.bag as db
import random
NUM_SAMPLES=100
def inside(p):
x, y = random.random(), random.random()
return x*x + y*y < 1
def calc_pi():
count = db.from_sequence(range(0, NUM_SAMPLES)).filter(inside).count().compute()
return 4.0 * count / NUM_SAMPLES
print(calc_pi())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment