function x=chi2rv(df); % chi2rv(df) generates a random number from a chi-square distribution % with df degrees of freedom; % See G.S. Fishman, 1973, Concepts and Methods in Discrete Event % Digital Simulation, Wiley, New York, p. 213. % REQUIRES THE FILES betarv.m AND gammarv.m if mod(df,2)==0 x=-2*log(prod(rand(1,df/2))); else k=(df/2)-0.5; v=sum(-log(rand(1,k))); y=betarv(0.5,0.125,1); z=-log(rand); x=2*(v+y*z); end;