function x = Rayliegh_rnd(muX, r, c)
% x = Rayleigh_rnd(modeX, r, c)
%
% generate a matrix of a random variable distribued according to the 
% Rayleigh distribution with mean muX 
%
%  INPUT         DESCRIPTION
%   muX          mean of the Rayleigh distribution            1 x 1   or  r x c
%   r            number of rows in x                                      1 x 1
%                sample of standard normal variables, possibly correlated r x c
%   c            number of cols in x                                      1 x 1
%
%  OUTPUT
%   x            sample of the Rayleigh distribution                      r x c
%
%  EXAMPLE 
%    To plot the PDF of a single Rayleigh random variable, ... 
%    n=10000; x=Rayleigh_rnd(1,1,n); hist(x,n/100,1); avg_x = sum(x)/n

%    Reference:   http://en.wikipedia.org/wiki/Exponential_distribution

 if any(muX<=0) || any(muX==Inf)
    error(' Rayleigh_rnd: muX must be greater than zero')
 end

 if nargin == 2
   u = norm_cdf(r);
   [r,c] = size(u);
 end
 if nargin == 3
   u = rand(r,c);
 end

 modeX = muX*sqrt(2/pi); 

 if (prod(size(modeX)) == 1), modeX = modeX * ones(r,c); end

 x = modeX .* sqrt(-2.0 * log( u ));

% --------------------------------------------------------------  Rayleigh_rnd
% HP Gavin 2016-04-29, 2021-04-26, 2023-12-22
