function average ( N )

%*****************************************************************************80
%
%% AVERAGE uses a Monte Carlo approach to estimate pi.
%
%  Modified:
%
%    27 January 2018
%
%  Author:
%
%    Paul Nahin
%
%  Reference:
%
%    Paul Nahin,
%    Digital Dice: Computational Solutions to Practical Probability Problems,
%    Princeton, 2008,
%    ISBN: 978-0-691-15821-1.
%
%  Parameter:
%
%    Input, integer N, the number of random samples.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'AVERAGE:\n' );
  fprintf ( 1, '  Use a Monte Carlo sample to estimate pi,\n' );

  sum = 0.0;
  for loop = 1 : N
    x = rand ( );
    sum = sum + sqrt ( 1.0 - x * x );
  end

  sum = 4.0 * sum / N;

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Estimate for pi = %g\n', sum );
  fprintf ( 1, '  Error =           %g\n', pi - sum );

  sum = 0.0;
  for loop = 1 : N
    x = rand ( );
    sum = sum + ( sqrt ( 1.0 - x * x ) + sqrt ( 1 - ( 1 - x )^2 ) ) / 2;
  end

  sum = 4.0 * sum / N;

  fprintf ( 1, '  Antithetic estimate for pi = %g\n', sum );
  fprintf ( 1, '  Error =                      %g\n', pi - sum );

  return
end
