In [43]:
import random
import numpy as np
import matplotlib.pyplot as plt

In [48]:
x = [] # absolute difference
y = [] # relative frequency
heads = 0
for total in xrange(1, 100000):
    if random.randint(0, 1) == 0:
        heads += 1
    x.append(heads - total / 2)
    y.append(1.0 * heads / total)
x = np.asarray(x)
x = np.cumsum(x)
y = np.asarray(y)
plt.plot(x)
plt.plot(y)
plt.show()

print y[-1]


0.50304503045

In [ ]: