function average ( trial_num )

%*****************************************************************************80
%
%% average() uses a Monte Carlo approach to estimate pi.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  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.
%
%  Input:
%
%    integer trial_num, the number of trials.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'average():\n' );
  fprintf ( 1, '  Use a Monte Carlo sample to estimate pi.\n' );

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

  sum = 4.0 * sum / trial_num;

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

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

  sum = 4.0 * sum / trial_num;

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

  return
end
