function lastx = walk(n) % random walk y = 2*unidrnd(2,1,n)-3; s = zeros(1,n); s(1) = y(1); lastx = 0; for i=2:n s(i) = s(i-1) + y(i); if s(i)==0 lastx = i; endif endfor plot(s,'b-',[0,n], [0,0], 'r-', [lastx], [0], 'g*'); tstring = [ int2str(n) " steps, " "last crossing at " int2str(lastx)]; title( tstring, "fontsize", 18 ); endfunction