function [ s, n2 ] = chebyshev2_sample ( n )

%*****************************************************************************80
%
%% CHEBYSHEV2_SAMPLE samples the Chebyshev2 PDF.
%
%  Discussion:
%
%    The Chebyshev2 PDF uses the density
%
%      rho(x) = 2/pi * sqrt(1-x^2)
%
%    for -1 <= X <= 1.
%
%    This PDF is easy to work with, but we want to use it as a demonstration
%    of rejection sampling.  In particular, we can compare this PDF to
%    a uniform PDF with maximum value 1.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    09 August 2016
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, integer N, the number of points to compute.
%
%    Output, real S(N), the sample points.
%
%    Output, integer N2, the number of trials required to produce
%    N acceptable points.
%
  pdfmax = 2.0 / pi;

  s = zeros ( n, 1 );

  i = 0;
  j = 0;

  while ( i < n )

    i = i + 1;

    while ( true )

      j = j + 1;
%
%  Select X at random in [-1,+1]
%
      x = - 1.0 + 2 * rand ( 1, 1 );
%
%  Select Y at random in [0,PDFMAX].
%
      y = pdfmax * rand ( 1, 1 );
%
%  Evaluate Z 
%
      z = 2.0 / pi * sqrt ( 1.0 - x .^ 2 );
%
%  Acceptance test:
%
      if ( y <= z )
        break
      end

    end

    s(i) = x;

  end

  n2 = j;

  return
end

