n = 100; % number of steps P = zeros(n,1); %position(time) vector P(1) = 0; % Starting value for i=2:n R = rand; if R < 0.5 S = -1; elseif R > 0.5 S = 1; end P(i) = S+P(i-1); end plot(1:n,P) ylabel('Position') xlabel('Step Count') title('1-D Random Walk')