Skip to content

Instantly share code, notes, and snippets.

@null-none
Created May 27, 2020 08:42
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 null-none/896b5451735ed7202e86b1c0bf33ff10 to your computer and use it in GitHub Desktop.
Save null-none/896b5451735ed7202e86b1c0bf33ff10 to your computer and use it in GitHub Desktop.
Code example: Estimating Pi with Fiber
from fiber import Pool
import random
NUM_SAMPLES = int(1e6)
def is_inside(p):
x, y = random.random(), random.random()
return x * x + y * y < 1
def main():
pool = Pool(processes=4)
pi = 4.0 * sum(pool.map(is_inside, range(0, NUM_SAMPLES))) / NUM_SAMPLES
print("Pi is roughly {}".format(pi))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment