MATLAB: Adding Bias To Random Walk

biasprobabilityrandom walk

How would add a bias or probability of 34%, 22%, 22%, and 22% for each of the four directions in the attached random walk code?

Best Answer

you can make a step & bias vector:
stepCoordinates = [1,0;0,1;-1,0;0,-1];
% this creates the bias to the north
bias = [ones(1, 34), repmat([2 3 4], 1, (100-34)/3)];
then when you generate the actual steps in your loop use this instead of what you had:
stepBiasIndex = randi(100, 1, numberOfSteps);
stepCoordIndex = bias(stepBiasIndex);
delta = stepCoordinates(stepCoordIndex,:);
if continuousSteps
signOfStep = sign(delta);
plusminusRandSign = [-1 1];
indicesWithSignZero = signOfStep == 0;
signOfStep(indicesWithSignZero) = plusminusRandSign(randi(2, size(signOfStep(indicesWithSignZero))));
delta = delta - signOfStep.*rand(numberOfSteps, 2);
end
deltax = delta(:,2)';
deltay = delta(:,1)';
You can vectorize the rest of it but you said it does what you want so i didn't touch anything else
In this specific solution the bias is only in percentage because the bias vector is a 1x100 vector
so if you want to be more specific about it you can make it a 1x1000 for promils or 1x10000 for higher precision...
I attached the edited file