function [xFinal, yFinal]= randomWalkIf(n,x0,y0)
% (xFinal,yFinal) is the location after n steps of random walk starting from (x0,y0).
% In each step, the possible moves are left, right, up, or down.

fprintf('Do a random walk starting from (%.1f, %.1f)!\n', x0, y0)
x= x0;  y= y0;  %coordinates throughout the walk

% Perform n steps of random walk
for k= 1:n
    r= rand(1,1);  % a random # in (0,1)
    if (r<0.25)
        x= x - 1;  % left
    elseif (r<0.5)
        x= x + 1;  % right
    elseif (r<0.75)
        y= y + 1;  % up
    else
        y= y - 1;  % down
    end
end 

xFinal= x;  yFinal= y;
fprintf('End up at (%.1f,%.1f) after %d steps!\n', xFinal, yFinal, n)
