function p = obtuse2 ( )

%*****************************************************************************80
%
%% obtuse2 estimates the probability of an obtuse triangle.
%
%  Discussion:
%
%    A stick of length 1 is randomly broken into 3 pieces.
%    What is the probablity that an obtuse triangle is formed?
%
%    Here, we choose x and y at random, and use these to determine
%    the lengths of the sides.
%
%  Modified:
%
%    08 May 2019
%
%  Author:
%
%    Paul Nahin
%
%  Reference:
%
%    Paul Nahin,
%    Will You Be Alive 10 Years From Now?,
%    Princeton, 2014,
%    ISBN: 978-0691156804,
%    LC: QA273.25.N344
%
%  Parameters:
%
%    Output, real p, the probability of creating an obtuse triangle.
%
  obtuse = 0;
  triangle = 0;
  triangle_num = 10000000;

  while ( triangle < triangle_num )

    x = rand ( );
    y = rand ( );

    if ( x < y )
      s1 = x;
      s2 = y - x; 
      s3 = 1.0 - y;
    else
      s1 = y;
      s2 = x - y;
      s3 = 1.0 - x;
    end

    if ( s1 < s2 && s2 < s1 + s3 && s3 < s1 + s2 )
      triangle = triangle + 1;
      d1 = s1^2;
      d2 = s2^2;
      d3 = s3^2;
      if ( d1 > d2 + d3 || d2 > d1 + d3 || d3 > d1 + d2 )
        obtuse = obtuse + 1;
      end
    end
  end

  p = obtuse / triangle_num;

  return
end
