function x = zipf_sample ( a )

%*****************************************************************************80
%
%% zipf_sample() samples the Zipf PDF.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    22 September 2004
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Luc Devroye,
%    Non-Uniform Random Variate Generation,
%    Springer Verlag, 1986, pages 550-551.
%
%  Input:
%
%    real A, the parameter of the PDF.
%    1.0 < A.
%
%  Output:
%
%    integer X, a sample of the PDF.
%
  b = 2.0 ^ ( a - 1.0 );

  while ( true )

    u = rand ( 1, 1 );
    v = rand ( 1, 1 );
    w = floor ( 1.0 / u^( 1.0 / ( a - 1.0 ) ) );

    t = ( ( w + 1.0 ) / w )^( a - 1.0 );

    if ( v * w * ( t - 1.0 ) * b <= t * ( b - 1.0 ) )
      break
    end

  end

  x = floor ( w );

  return
end
