MATLAB: How to run this simulation 10000 times

MATLABmultiple simulations

Hello. I am currently working on a random walk scenerio, and this is my code for running the random walk program once.
for z = 2:1001
w = rand(1001,1);
if (w(z) >= .5)
Left(z) = Left(z-1) + 1;
Right(z) = Right(z-1);
else
Right(z) = Right(z-1) + 1;
Left(z) = Left(z-1);
end
end
Y = Left - Right;
However, I want to run this random walk program 10000 times and record where the random walk simulation ends up. In addition, I want to store where each simulation ends up at to put into a histogram. I tried to use another for loop to run the simulation, but I couldn't get it to work. Any help would be appreciated.
n = 10001;
result = zeros(n,1);
for k = 1:n
for z = 2:1001
w = rand(1001,1);
if (w(z) >= .5)
Left(z) = Left(z-1) + 1;
Right(z) = Right(z-1);
else
Right(z) = Right(z-1) + 1;
Left(z) = Left(z-1);
end
end
Y = Left - Right;
end
result(k) = Y

Best Answer

The assignment to result should be within the outer loop.