function X = chi2rnd(k,R,C)
% X = chi2rnd(k,R,C)
% compute a sample of a chi-squared distribution with k degrees of freedom
% using the Wilson-Hilferty transformation
% https://en.wikipedia.org/wiki/Chi-squared_distribution#Asymptotic_properties 

% Wilson-Hilferty transformation

m = 1 - 2 / (9*k);    % mean of cube root of X / k
s = sqrt(2/(9*k));    % standard deviation of cube root of X / k

X = k*(m + s*randn(R,C)).^3;
 
% ----------------------------------------------------------------------
