function flysquare ( )

%*****************************************************************************80
%
%% FLYSQUARE simulates two flies landing at random in the unit square.
%
%  Discussion:
%
%    Two flies land at random spots in the unit square.
%    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, 'FLYSQUARE\n' );
  fprintf ( 1, '  Two flies land at random spots on the unit square.\n' );
  fprintf ( 1, '  What is the probability they are more than 1 unit apart?\n' );

  summation = 0;
  for fly = 1 : 100000
    x1 = rand ( );
    y1 = rand ( );
    x2 = rand ( );
    y2 = rand ( );
    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
nd
