function [x y] = RandomWalk2D(N)
% N is a positive integer.
% Simulates a 2D random walk that continues until
% the absolute value of the x-coordinate or y-coordinate of the token
% is equal to N.
% x and y are row vectors with the property that (x(k),y(k)) is the
% location of the token after k hops, k=1:length(x).

% Initializations...
k = 0; xc = 0; yc = 0;

% In general, (xc,yc) is the location after k hops.

while abs(xc)<N && abs(yc)< N
% Standing at (xc,yc).
% Flip coins to determine new location...
if rand < .5
if rand < .5
xc = xc + 1;
else
xc = xc - 1;
end
else
if rand < .5
yc = yc + 1;
else
yc = yc - 1;
end
end
% Save location...
k = k + 1; x(k) = xc; y(k) = yc;
end