-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16_5.py
29 lines (26 loc) · 909 Bytes
/
16_5.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
import random
def throwNeedles(numNeedles):
inCircle = 0
for Needles in range(1, numNeedles + 1):
x = random.random()
y = random.random()
if (x*x+y*y)**0.5 <= 1:
inCircle += 1
#1つの象限内の針のみ数えるため、4倍する
return 4*(inCircle/float(numNeedles))
def getEst(numNeedles, numTrials):
estimates = []
for t in range(numTrials):
piGuess = throwNeedles(numNeedles)
estimates.append(piGuess)
sDev = stdDev(estimates)
curEst = sum(estimates)/len(estimates)
print('Est =' + str(round(curEst, 5))+ ',', 'Std. dev =' + str(round(sDev, 5))+',', 'Needles ='+ str(numNeedles))
return (curEst, sDev)
def estPi(precision, numTrials):
numNeedles = 1000
sDev = precision
while sDev >= precision/1.96:
curEst, sDev = getEst(numNeedles, numTrials)
numNeedles *= 2
return curEst