function x = GEV_rnd ( param, r, c )
% x = GEV_rnd (  [m,s,k], r, c )
% generate a matrix of a random variable distribued according to the 
% generalized extreme value (GEV) distribution 
% with parameters m, s, and k and CDF
%  F =  exp(-(1+k*z).^(-1/k) ); where  z = ( x - m ) / s;
% usually -0.5 < k < +0.5
%
%  INPUT         DESCRIPTION
%  param         distribution parameter vector [ m , s , k ]              3 x 1
%   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 generalized extreme value distribution         r x c
%
%  EXAMPLE 
%    To plot the PDF of a single extreme type I random variable, ... 
%    n=10000; x=GEV_rnd([3,1,0.2],1,n); hist(x,n/100,1); avg_x = sum(x)/n

 m = param(1);  % shift
 s = param(2);  % scale
 k = param(3);  % shape

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

 x = m + (s/k) * ( (-log(u)).^(-k) - 1 );

% --------------------------------------------------------------------- GEV_rnd
% HP Gavin, 31 Feb 2013,  2019-11-20, 2021-04-26

