function [ Position ] = ProbGen( ProbVec )

P = rand;            % P is a random number between 0 and 1
l = length(ProbVec);
CumulProb = 0;       
for i=1:l
    CumulProb = CumulProb + ProbVec(i); % We add up the cumulative 
    if P < CumulProb                    % probabilities until the first
        Position = i;                   % time they exceed P
        break;                          % At this point we break out of the
    end                                 % loop and the function returns.
end

