MATLAB: Generating 100 people walk 100 steps and show there final position on a histogram

histogramMATLAB

%% Initial Values
x=[50,100];
steps=100;
position=0;
blackhole=100;
speedzone=-30;
dead=0;
%% for loop
for k=1:length(x)
d=zeros(1,x(k));
for j=1:x(k)
for i=1:steps
length=rand(1,2);
secondlength=rand;
s=rand(1,2);
if s==1
position=position+length;
elseif s==2
position=position-length;
end
if position>blackhole
dead=dead+1;
elseif position<speedzone && s==1
position=position+secondlength;
elseif position<speedzone && s==2
position=position-secondlength;
end
end
d(j)=position;
end
figure,hist(d,50),title([num2str(x(k)),' people taking 2500 steps'])
end
Everytime I run this I get two histograms with only 50 and 100 final positions of 0. If anyone can help that'd be great, Thanks.

Best Answer

a=randi(2,100,10000);%added a few more people
a(a==2)=-1;%1==+step, 2==-step
histogram(sum(a),40);