Created
May 27, 2020 08:42
-
-
Save null-none/896b5451735ed7202e86b1c0bf33ff10 to your computer and use it in GitHub Desktop.
Code example: Estimating Pi with Fiber
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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