Professional Documents
Culture Documents
Coin
Coin
numpy as np
import matplotlib.pyplot as plt
import random
N=1000
M=20000
def coin(N):
h=t=0
for i in range(N):
r=random.choice(["H","T"])
if (r=="H") :
h=h+1
else:
t=t+1
return h,t
def s(M):
H,T,T1=[],[],[]
for i in range (M):
h1,t1=coin(N)
H.append(h1)
T.append(t1)
T1.append(t1+150)
return H,T,T1
x,y,z=s(M)
plt.hist(x,50,color="white",ec="k")
plt.hist(z,50,color="white",ec="k")
plt.grid(which='major')
plt.minorticks_on()
plt.grid(which='minor', linestyle='dotted')
plt.show()
print("Probability of head=",np.mean(x)/N)
print("Probability of tail=",np.mean(y)/N)
from IPython.display import set_matplotlib_formats
set_matplotlib_formats('pdf', 'svg')