function x = chi_square_sample ( a )

%*****************************************************************************80
%
%% chi_square_sample() samples the central Chi squared PDF.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    08 October 2004
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    real A, the parameter of the PDF.
%    1 <= A.
%
%  Output:
%
%    real X, a sample of the PDF.
%
  it_max = 100;

  n = floor ( a );

  if ( n == a && n <= it_max )

    x = 0.0;
    for i = 1 : n
      x2 = normal_01_sample ( );
      x = x + x2 * x2;
    end

  else

    a2 = 0.0;
    b2 = 1.0;
    c2 = a / 2.0;

    x = gamma_sample ( a2, b2, c2 );

    x = 2.0 * x;

  end

  return
end
