Straight forward way

Clever vectorised way

partnumber=1000;

particlemat=zeros(partnumber,2);

loops=1000;

 

for i=1:loops

for j=1:partnumber

X=random('unid',4);

if X==1

particlemat(j,1)=particlemat(j,1)-1;

end

if X==2

particlemat(j,2)=particlemat(j,2)-1;

end

if X==3

particlemat(j,1)=particlemat(j,1)+1;

end

if X==4

particlemat(j,2)=particlemat(j,2)+1;

end

end

end

numb=10000;

patmat=zeros(numb,2);

for i=1:1000

dx=random('bino',1,0.5,numb,1)+1;

dy=random('bino',1,0.5,numb,1)+1;

xm=random('bino',1,0.5,numb,1).*dx;

ym=~xm.*dy;

xm(xm==1)=-1;

xm(xm==2)=1;

ym(ym==1)=-1;

ym(ym==2)=1;

patmat(:,1)=patmat(:,1)+xm;

patmat(:,2)=patmat(:,2)+ym;

end