Toss a coin 5000 times; after each toss, calculate the ratio of heads to all tosses.
Graph the ratio against time; the X axis is the toss number, the Y axis is the ratio of heads to all tosses at that point in time.
# Simululation: Toss a Coin
# Toss a coin 5000 times; after each toss, calculate the ratio of heads to all tosses.
# Graph the ratio against time; the X axis is the toss number, the Y axis is the ratio of heads to all tosses at that point in time.
# Lauri Ruotsalainen, 2010
import random
@interact
def coin(n = input_box(default=5000, label="Number of Tosses"), interval = range_slider(vmin=0.0, vmax=1.0, default=(0.47, 0.53), step_size=0.01, label="Plotting range (y)")):
c = []
k = 0
for i in range(1, n):
k += random.randint(0, 1)
c.append((i, k/i))
show(point(c, gridlines=[None, [0.5]], pointsize=1), ymin=interval[0], ymax=interval[1])