function out = tnormrnd_robert_single(mu,sig,l)
%
%
% PURPOSE: returns a single random draw from the truncated normal
% distribution with mean mu, standard deviation sig, and 
% left truncation point l, using Robert's
% (1995) efficient accept-reject algo
%---------------------------------------------------
% INPUTS: 
% where: mu = mean of the (untruncated) normal
%        sig = standard deviation 
%        l = left trunction point
%---------------------------------------------------
% OUTPUT:
% a vector of random draws from the truncated normal distribution     
%
% Note: For double truncation use tnormrnd_robert_double; for right
% truncation use -tnormrnd_robert_single(-mu,sig,-u), 
% where u is the right (upper) truncation point;
%
% written by:
% Klaus Moeltner
% University of Nevada, Reno

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% standardize to standard normal

L=((l-mu)/sig);
a=(L+(sqrt(L^2+4)))/2;

z=(-log(rand)/a)+L; %equivalent to next line, but MUCH faster
% z=exprnd(1/a)+L; 

rz=exp(-(z-a)^2/2);
i=rz-rand;

while i<0
%   z=exprnd(1/a)+L;
    z=(-log(rand)/a)+L;
    rz=exp(-((z-a)^2/2));
    i=rz-rand;
end

out=z*sig+mu;

    

