function flycircle ( )

%*****************************************************************************80
%
%% FLYCIRCLE models two flies landing at random points in the unit circle.
%
%  Discussion:
%
%    Two flies land at random spots in the unit circle.
%    What is the probability they are more than 1 unit apart?
%
%  Modified:
%
%    29 January 2018
%
%  Author:
%
%    Paul Nahin
%
%  Reference:
%
%    Paul Nahin,
%    Dueling Idiots and Other Probability Puzzlers,
%    Princeton, 2012,
%    ISBN: 978-0691155005.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'FLYCIRCLE\n' );
  fprintf ( 1, '  Two flies land at random spots in the unit circle.\n' );
  fprintf ( 1, '  What is the probability they are more than 1 unit apart?\n' );

  summation = 0;
  radius = 1 / sqrt ( pi );
  factor = 2 * pi;

  for fly = 1 : 100000
    r1 = rand ( ) * radius;
    a1 = rand ( ) * factor;
    r2 = rand ( ) * radius;
    a2 = rand ( ) * factor;
    x1 = r1 * cos ( a1 );
    y1 = r1 * sin ( a1 );
    x2 = r2 * cos ( a2 );
    y2 = r2 * sin ( a2 );
    distance2 = ( x1 - x2 ) ^ 2 + ( y1 - y2 ) ^ 2;
    if ( distance2 >= 1 )
      summation = summation + 1;
    end
  end

  summation = summation / 100000;

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Probability flies are more than 1 unit apart = %g\n', summation );

  return
end
