clear clc x = 4; steps = zeros(1,100); for i = 1 :100000 counter = 0; x = 4; while (x>1 && x<7) r = rand; if r<0.5 x=x-1; else x=x+1; end counter = counter + 1; end steps(i)=counter; end mean(steps)