function first_survives_prob = duel2 ( trial_num )

%*****************************************************************************80
%
  first_survives_num = 0;
%
%  Assume bullet is always in chamber 6.
%
  for trial = 1 : trial_num

    shot_max = 1;
    shooter = 1;
    done = 0;

    while ( ~done )

      for shot = 1 : shot_max

        i = i4_random ( 1, 6 );

        if ( i == 6 )
          done = 1;
          if ( mod ( shot_max, 2 ) == 1 )
            first_survives_num = first_survives_num + 1;
          end
          break
        end

      end
%
%  Shooter missed.
%    Increase number of shoots.
%    Switch shooters.
%
      shot_max = shot_max + 1;
      shooter = 3 - shooter;

    end

  end

  first_survives_prob = first_survives_num / trial_num;

  return
end
function value = i4_random ( lo, hi )

%*****************************************************************************80
%
%% I4_RANDOM returns a random integer between LO and HI.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    22 March 2009
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, integer LO, HI, the limits.
%
%    Output, integer VALUE, the random integer.
%
  r = ( hi + 1 - lo ) * rand ( );
  value = lo + floor ( r );

  return
end
