function PlotDistances()
% Displays distance distribution for random walks with N = 20,25,30,35,40

close all
% To get a good average, repeat the random walk this number of
% times for each N:
nWalks = 200;

for N = 20:5:40
% Display distribution for random walk of size N
% We will accumulate the distributions in D_sum
D_sum = zeros(N,1);
for walk=1:nWalks;
D = Distribution(N);
% Add in the results from this random walk...
D_sum = D + D_sum;
end
%Compute and display the average distribution...
D_ave = D_sum/nWalks;
figure
bar(1:N,D_ave)
axis([0 N 0 max(D_ave)+1])
title(sprintf('N = %2d',N))
end

function D = Distribution(N)
% Performs a random walk of size N.
% D is a N-by-1 vector with the property that D(k) is the t
% number of times that the token was on the k-th ring.
[x,y] = RandWalk(N);
D = zeros(N,1);
for i=1:length(x)
% After the i-th hop, the token is on the k-th ring...
k = max(abs(x(i)),abs(y(i)));
if k > 0
D(k) = D(k)+1;
end
end