% hw025.m
% John Burkardt
% Probability that 3 pieces of a (unit) stick will form a triangle.
%
n = input ( 'Enter number of trials: ' );

m = 0;

for i = 1 : n
%
%  Let P1 and P2 be randomly chosen points on the stick.
%
  p1 = rand ( );
  p2 = rand ( );
%
%     X               Y               Z
%  0-----min(P1,P2)------max(P1,P2)------1
%
  x = min ( p1, p2 );
  y = max ( p1, p2 ) - min ( p1, p2 );
  z = 1.0 - max ( p1, p2 );

  if ( ...
    ( z <= x + y ) && ...
    ( y <= x + z ) && ...
    ( x <= y + z ) )
    m = m + 1;
  end

end

fprintf ( '  Using %d trials, estimated probability is %g\n', n, m / n );
