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

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