% Flip a coin a lot of times, and track the excess number of heads.

n=100000 % Number of flips
p=.5 % Probability of heads

flip=rand(1,n)<p;
headcount=cumsum(flip); % Running tally of heads

excess=headcount-[1:n]*p; % Excess number of heads
figure(1)
plot(excess)
title('Excess number of heads')

frac=headcount./[1:n]; % Fraction of heads
figure(2)
plot(frac-p)
axis([1 n -.1 .1]) %Zoom in
title('Excess fraction of heads')