size=301; ar=zeros(size+2,size+2); ar(size,:)=100*ones(1,size+2); vd=1; figure('Position',[100,100,400,400]) q=plot(size,((size+1)/2),'EraseMode','none','Color','red') axis([0 size 0 size]) pts=0; for i=1:100000 ch=rand(1); x=1; y=floor(ch*size+1); for j=1:10000 xlast=x; ylast=y; ch=rand(1); if (ch < .25 ) x=x+1; elseif (ch >=.25 & ch < .5) x=x-1; elseif (ch >= .5 & ch < .75) y=y+1; else y=y-1; end if (x < 2 | y < 2 | x > size | y > size) break end if (ar(x,y) == 100 | ar(x,y+1) == 100 | ar(x,y-1) == 100) ar(x-1,ylast)=100; pts=pts+1; [i,pts] set(q,'XData',x-1,'YData',ylast) drawnow break end x=x+vd; if (x < 2 | y < 2 | x > size | y > size) break end if (ar(x,y) == 100 | ar(x,y+1) == 100 | ar(x,y-1) == 100) ar(x-1,ylast)=100; pts=pts+1; [i,pts] set(q,'XData',x-1,'YData',ylast) drawnow break end end end