import numpy as np
import matplotlib.pyplot as plt


n       = 100             # the number of flips
p       = 0.5            # bernoulli's success probability
sample  = 100            # sample times
S = np.random.binomial(n, p, sample)

plt.figure('Probability Distribution')
plt.hist(S, normed=True, rwidth=1, bins=range(0,n+1))
plt.xlabel('x')
plt.ylabel('Pr[X=x]')
plt.xticks(np.arange(0,n+1))
plt.title('Binomial sample %d times with n = %d and p = %.2f. Mean = %.2f.' % (sample, n, p, np.mean(S)))
plt.grid()
plt.show()
