This repository has been archived on 2021-05-17. You can view files and clone it, but cannot push or open issues or pull requests.
scientificComputing/programming/exercises/randomwalkthresh.m

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