%***********************************************************
%        Random variable Generator 
%        This mfunction generates random L random numbers 
%        that are distributed with a specified distribution 
%        and specified parameters. 
%        AUTHOR: Balu Santhanam
%        DATE  : 09/10/00 
%        FUNCTION SYNOPSIS: 
%        x = rndv(L,para,type)
% 
%        Parameter Description: 
%        L       :   number of samples  
%        para    :   parameter vector 
%        type    :   string containing the type 
%                    of RV needed with the following options
%        'gauss' :   Gaussian with parameters [mu,sigma]
%        'unif'  :   Uniform with parameters : [a,b].
%        'betad' :   Beta with parameters : [m,n]
%        'lapl'  :   Laplacian with parameters : [mu,sigma]
%        'expn'  :   Exponential with parameter lambda
%        'weib'  :   Weibull distribution with parameters [a,b] 
%**********************************************************
function x = rndv(L,para,type)
u = rand(1,L); 
switch type
case 'gauss' ; %  Case where a Gaussian is requested
   mu = para(1); sigma = para(2);
   x = norminv(u,mu, sigma);
   fprintf('%s\t%f\t%f\n','Gaussian with parameters',mu,sigma)
case 'unif' ;  %  Case where a uniform distr. is needed
   a = para(1); b = para(2); 
   x = u*(b-a) + a;
   fprintf('%s\t%f\t%f\n','Uniform with parameters',a,b)
case 'betad' ; % Case where a Beta distribution is needed
   m = para(1); n = para(2);    
   x = betainv(u,m,n);
   fprintf('%s\t%f\t%f\n','Beta with parameters',m,n)
case 'lapl' ; % When laplacian distribution is needed
   mu = para(1); sig = para(2);
   index1 = find(u < 0.5);
   index2 = find(u >= 0.5);
   x(index1) = log(2*u(index1));
   x(index2) = -log(2*(1-u(index2))); 
   x = x*sig + mu;
   fprintf('%s\t%f\t%f\n','Laplacian with parameters:',mu,sigma)  
case 'expn' ;  % When a exponential distribution is needed
   lambda = para(1);   
   x = -(1/lambda)*log(1-u); 
   fprintf('%s\t%f\n','Exponential with parameter',lambda)
case 'weib' ; % When a Weibull distribution is called for
   x = weibinv(u,para(1),para(2));
   fprintf('%s\t%f\t%f\n','Weibull with parameters:',para(1),para(2))
end;




