function idiots1 ( )

#*****************************************************************************80
#
## idiots1() models a duel between two idiots.
#
#  Discussion:
#
#    A and B fight a duel, but they only have one gun, a six-shot revolver.
#    A will spin the cylinder and fire at B.
#    If the gun doesn't fire, then B will spin the cylinder and fire at A.
#    They continue to alternate until one of them is shot.
#    What is the probablity that A will win?
#    What is the average number of trigger pulls?
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    08 February 2023
#
#  Author:
#
#    Original MATLAB version by Paul Nahin.
#    This version by John Burkardt.
#
#  Reference:
#
#    Paul Nahin,
#    Dueling Idiots and Other Probability Puzzlers,
#    Princeton, 2012,
#    ISBN: 978-0691155005.
#
  print ( '' )
  print ( 'idiots1():' )
  print ( '  A and B fight a duel, but they only have one gun, a six-shot revolver.' )
  print ( '  A will spin the cylinder and fire at B.' )
  print ( '  If the gun does not fire, then B will spin the cylinder and fire at A.' )
  print ( '  They continue to alternate until one of them is shot.' )
  print ( '  What is the probablity that A will win?' )
  print ( '  What is the average number of trigger pulls?' )
#
#  Duels = total number of duels.
#
  Duels = 10000
  duration = zeros(1,100)      # assume no duel exceeds 100 trigger-pulls
  a = 0                       # number of times A has won
  nd = 0                      # number of duels completed
  tp = 0                      # number of trigger pulls so far

  while ( nd < Duels )

    tp = tp + 1               #A pulls the trigger
    if ( rand ( ) <= 1 / 6 )   #A wins
      duration(tp) = duration(tp) + 1
                               #duel is over, update the duration vector
      tp = 0                  #initialize trigger-pulls for next duel
      a = a + 1
      nd = nd + 1
    else
      tp = tp + 1             #B pulls the trigger
      if ( rand ( ) <= 1 / 6 ) #B wins
        duration(tp) = duration(tp) + 1
                               #duel is over, update duration vector
        tp = 0                #initialize trigger-pulls for next duel
        nd = nd + 1
      end
    end

  end 

  a = a / Duels
  average = 0
  for k = 1 : length ( duration )
    average = average + k * duration(k)
  end
  average = average / Duels

  print ( '' )
  print ( '  The probability A wins is', a )
  print ( '  The average number of trigger-pulls/duel is', average )
#
#  Graphics.
#
  clf ( )
  bar ( duration )
  title ( 'Relative Frequency of the Number of Trigger-Pulls per Duel')
  xlabel ( 'duration of duels, in units of trigger-pulls' )
  ylabel ( 'number of duels' )
  grid ( True )
  filename = 'idiots1.png'
  plt.savefig ( filename )
  print ( '  Graphics saved as "' + filename + '"' )
  plt.close ( )

  return
end

