26 lines
487 B
Matlab
26 lines
487 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];
|
|
i = 2;
|
|
while true
|
|
r = rand(1);
|
|
if r < p
|
|
positions(i) = positions(i-1) + 1;
|
|
else
|
|
positions(i) = positions(i-1) - 1;
|
|
end
|
|
if abs(positions(i)) > thresh
|
|
break
|
|
end
|
|
i = i + 1;
|
|
end
|
|
end
|