-
Notifications
You must be signed in to change notification settings - Fork 23
/
pi.py
31 lines (25 loc) · 859 Bytes
/
pi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/env python3
from random import random
from math import pow
import argparse
def compute_pi(attempts):
"""
This is a monte carlo method for estimating the value of pi
"""
inside = 0
tries = 0
# Try the specified number of random points
while (tries < attempts):
tries += 1
if ( (pow(random(),2) + pow(random(),2)) < 1):
inside += 1
# Compute the ratio and return
result=4*(inside/tries)
return(result)
if __name__ == '__main__':
# Use argparse to take command line options and generate help text
parser = argparse.ArgumentParser()
parser.add_argument("attempts", help="number of random points (int)", type=int)
args = parser.parse_args()
result = compute_pi(args.attempts)
print( f'Final pi estimate from {args.attempts} attempts = {result}' )