I am trying to create a simple random walk. This is the code that I wrote.
n=50;
p=0.5;
Y=zeros(n,1);
X=zeros(n,1);
X(1)=0;
for i=1:length(n)
Y(i,1)=(rand(1)<=p);
end
for i=1:length(n)
X(i+1)=X(i)+(2*Y(i)-1);
end
plot(1:n,X,'.-')
However, in this if I check Y
, which stores the random Bernoulli variables,I get all zeroes. Why does it happen?
I get a plot like, .
Which doesn't look like a random walk. Can someone please tell me what I am doing wrong