# script thwowCoin.py ''' example to produce random numbers (0/1) as obtained by throwing a coin .. author:: Guenter Quast ''' # pyhton2 - python3 compatibility from __future__ import division from __future__ import absolute_import from __future__ import print_function from __future__ import unicode_literals # import numpy as np import matplotlib.pyplot as plt nthrow=500 # how often to throw r=[] Nh=0 for N in range(nthrow): if np.random.rand()>0.5: Nh+=1 r.append(float(Nh)/(N+1.)) # plot result ... x=np.linspace(0.,nthrow,nthrow) plt.plot(x,r,'g.') # ... and expectation ... plt.plot(x,0.5*np.ones(nthrow),'r--',lw=2) # ... and error band plt.plot(x,0.5+0.5/np.sqrt(x),'b--',lw=2) plt.plot(x,0.5-0.5/np.sqrt(x),'b--',lw=2) plt.show()