function randomWalk(n)
% randomWalk(n): Display n steps of a random walk in the plane.

% Written by Paul Chew for CS100M, Feb 2006

x = zeros(1, n+1);      % Trajectory in x direction
y = zeros(1, n+1);      % Trajectory in y direction

for k = 1:n
    r = rand(1);
    if r < 0.25         % Right
        x(k+1) = x(k) + 1;
        y(k+1) = y(k);
    elseif r < 0.5      % Up
        x(k+1) = x(k);
        y(k+1) = y(k) + 1;
    elseif r < 0.75     % Left
        x(k+1) = x(k) - 1;
        y(k+1) = y(k);
    else                % Down
        x(k+1) = x(k);
        y(k+1) = y(k) - 1;
    end
end

% Show the walk
plot(x, y, '-', x(1), y(1), 'r*', x(end), y(end), 'ro')
axis('equal')
title([num2str(n) ' steps of random walk from * to o'])
