function M = mash(N) % Function to simulate random walk of Prof. Mash % Input: % N = number of steps % Output: % M = N x 2 matrix holding the location of Mash at each step along his % walk % Initialize M M = zeros(N,1); % First column of M = x coordinate and is always 1, 2, 3, ... , N M(:,1) = (1:N)'; % Start out with y position = 0 ypos = 0; % Loop over each step for idx = 1:N % Generate a random number between 0 and 1 r = rand(1); % If r < 1/3, move one m in -y direction. If r is between 1/3 and 2/3 % then movement in y direction. Else, move one step in +y direction if (r <= 1/3) ypos = ypos - 1; elseif (r > 2/3) ypos = ypos + 1; end M(idx,2) = ypos; end