25 lines
513 B
Matlab
25 lines
513 B
Matlab
function positions = randomwalkthresh(p, thresh)
|
|
% computes a single random walk
|
|
%
|
|
% Arguments:
|
|
% p: the probability for an upward step
|
|
% thresh: compute the random walk until abs(pos) is larger than thresh
|
|
%
|
|
% Returns:
|
|
% positions: vector with positions of the random walker
|
|
|
|
positions = [0.0];
|
|
% positions = 0.0;
|
|
% positions = zeros(1, 1);
|
|
i = 2;
|
|
while abs(positions(i-1)) < thresh
|
|
r = rand(1);
|
|
if r < p
|
|
positions(i) = positions(i-1) + 1;
|
|
else
|
|
positions(i) = positions(i-1) - 1;
|
|
end
|
|
i = i + 1;
|
|
end
|
|
end
|