%This function returns geometrically distributed random variable.
%Input parameter is the mean value of the distribution.
%Return value is always an integer, whereas mean value may be positive real value.
%On error cases -1 is returned.

function y=geometric(meansize, minsize, maxsize);

if meansize <= 0 
    y = -1;
    return 
end

y=0;

p=1/meansize; %Geometric distribution is the first success in a serieas of Bernoulli trials with probability p, and mean value is 1/p => thus p=1/meansize.


while or(y>maxsize,y<minsize)
    i=1;
    while rand > p,
        i=i+1;
    end
    y=i;
end