-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15_4.py
29 lines (28 loc) · 1007 Bytes
/
15_4.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
def flipPlot(minExp, maxExp):
"""minExpとmaxExpはminExp<maxExpを満たす正の整数とする
2**minExpからmaxExp回のコイン投げの結果をプロットする"""
ratios, diffs, xAxis = [], [], []
for exp in range(minExp, maxExp+1):
xAxis.append(2**exp)
for numFlips in xAxis:
numHeads = 0
for n in range(numFlips):
if random.choice('H', 'T') == 'H':
numHeads += 1
numTails = numFlips - numHeads
try:
ratios.append(numHeads/numTails)
diffs.append(abs(numHeads-numTails))
except ZeroDivisionError:
continue
pylab.title('Difference between Heads and Tails')
pylab.xlabel('Number of Flips')
pylab.ylabel('Abs(#Heads - #Tails)')
pylab.plot(xAxis, diffs, 'k')
pylab.figure()
pylab.title('Heads/Tails Ratios')
pylab.xlabel('Number of Flips')
pylab.ylabel('#Heads/#Tails')
pylab.plot(xAxis, ratios, 'k')
random.seed(0)
flipPlot(4, 20)