-
Notifications
You must be signed in to change notification settings - Fork 0
/
pi_generator.py
69 lines (61 loc) · 1.58 KB
/
pi_generator.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#
# A Monte-Carlo Simulation to estimate pi
#
# Josh Sarath
#
from math import sqrt,pi
from random import uniform
def getPoint():
"""
gets a randomly generated point
"""
x=uniform(0,1)
y=uniform(0,1)
return (x,y)
def withinCircle(x,y):
"""
determines if the randomly generated x,y point is within the circle of diameter height of square
"""
if sqrt(x**2 + y**2) <= 1:
return True
else:
return False
def approxPi(iterations,verbose):
"""
agregates each individual test of withinCircle to begin to approximate real area of the circle
"""
hit = 0
miss = 0
if verbose == True:
for i in range(iterations):
x,y= getPoint()
withinCircle(x,y)
if withinCircle(x,y):
z = "Hit"
hit = hit + 1
else:
z = "Miss"
miss = miss + 1
ratio = hit/(hit + miss)
pi = ratio*4
print((i), (x,y), (z), (pi)) #print out after each loop would get "verbose" for high iterations
if verbose == False:
for i in range(iterations):
x,y= getPoint()
withinCircle(x,y)
if withinCircle(x,y):
hit = hit + 1
z = "Hit"
else:
miss = miss + 1
z = "Miss"
ratio = hit/(hit + miss)
pi = ratio*4
print(pi)
approxPi(10, False)
def main():
import doctest
print("Testing...")
doctest.testmod()
if __name__ == '__main__':
main()