-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApproximate_Pi.py
61 lines (54 loc) · 1.5 KB
/
Approximate_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
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
# Fettet, Louis
# Approximate Pi Project
# 9/20/2011
from math import pi, fabs
from random import uniform
def getPoint():
"""
Returns a random point in first quandrant.
>>> (x,y)=getPoint()
>>> x*x <= 1 and y*y <= 1
True
"""
x = uniform(0,1)
y = uniform(0,1)
return (x, y)
def withinCircle(x, y):
"""
Determine whether the point (x,y) is within the unit circle
centered at (0,0)
>>> withinCircle(0.5,0.5)
True
>>> withinCircle(1,0.5)
False
"""
return (x * x) + (y * y) <= 1
def approxPi(iterations, verbose):
"""
This function accepts a number which will be the number of iterations
to run. And it returns its approximation of pi obtained after that
number of iteration.
The second parameters (verbose) indicates whether to print something
during the simulation.
The following test ensures that the function returns an acceptable
approximation.
>>> fabs(approxPi(1000,False)-pi) < 0.5
True
"""
hits = 0
for i in range (iterations):
(rx, ry) = getPoint()
if withinCircle(rx,ry):
hits += 1
if (verbose):
print ((rx, ry),(" HIT "), (4 * (hits / iterations)))
else:
if (verbose):
print ((rx, ry),(" MISS "), (4*(hits / iterations)))
return 4 *(hits / iterations)
def main():
import doctest
print ('Testing... please wait...')
doctest.testmod()
if __name__ == '__main__':
main()