function odd ( N )

#*****************************************************************************80
#
## odd() simulates the game of odd man out.
#
#  Discussion:
#
#    This code simulates 1,000 games of odd-person-out, for N people as
#    defined by the user, with each person flipping a fair coin. The elements 
#    of the row vector duration are the number of games of length i, i.e.,
#    duration(i)=# of games that require i flips to complete, where i=1,2,3...
#
#  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.
#
#  Input:
#
#    integer N, the number of players.  This should be a relatively
#    small number like 3, 4 or 5.
#
  print ( '' )
  print ( 'odd():' )
  print ( '  Simulate 1,000 games of odd-person-out, for N people.' )
  print ( '  Each person flips a fair coin. The elements ' )
  print ( '  of the row vector duration are the number of games of length i, i.e.,' )
  print ( '  duration(i)=# of games that require i flips to complete, where i=1,2,3...' )

  duration_size = 100
  duration = zeros ( 1, duration_size )
  duration_max = 0

  for game = 1 : 1000

    gameover = 0
    flip = 1

    while ( gameover == 0 )

      s = 0
      for n = 1 : N
        coin = rand ( )
        if ( coin < 1 / 2 )
          s = s + 1
        end
      end

      if ( s == 1 | s == N - 1 )
        gameover = 1
      else
        flip = flip + 1
      end

    end

    if ( flip <= duration_size )
      duration(flip) = duration(flip) + 1
    end

    duration_max = max ( duration_max, flip )

  end

  average = 0
  for i = 1 : length ( duration )
    average = average + i * duration(i)

  average = average / 1000

  print ( '' )
  print ( '  The average number of flips for #d players is #g', ...
    N, average )
#
#  Graphics.
#
  clf ( )
  bar ( duration(1:duration_max) )
  title ( 'Duration of Odd-Person-Out Games' )
  xlabel ( 'duration of games, in units of flips' )
  ylabel (' number of games' )
  grid ( True )
  filename = 'odd.png'
  plt.savefig ( filename )
  print ( '  Graphics saved as "' + filename + '"' )
  plt.close ( )

  return

