function odd ( trial_num, player_num )

%*****************************************************************************80
%
%% odd() simulates the game of odd man out.
%
%  Discussion:
%
%    This code simulates games of odd-person-out, for player_num 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 MIT license.
%
%  Modified:
%
%    29 January 2018
%
%  Author:
%
%    Paul Nahin
%
%  Reference:
%
%    Paul Nahin,
%    Dueling Idiots and Other Probability Puzzlers,
%    Princeton, 2012,
%    ISBN: 978-0691155005.
%
%  Input:
%
%    integer trial_num: the number of trials.
%
%    integer player_num: the number of players.  This should be a relatively
%    small number like 3, 4 or 5.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'odd():\n' );
  fprintf ( 1, '  Simulate %d games of odd-person-out,\n', trial_num ); 
  fprintf ( 1, '  for %d players.\n', player_num );
  fprintf ( 1, '  Each person flips a fair coin. The elements \n' );
  fprintf ( 1, '  of the row vector duration are the number of games of length i, i.e.,\n' );
  fprintf ( 1, '  duration(i)=# of games that require i flips to complete,\n' );
  fprintf ( 1, '  where i=1,2,3...,100\n' );

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

  for game = 1 : trial_num

    gameover = 0;
    flip = 1;

    while ( gameover == 0 )

      s = 0;
      for n = 1 : player_num
        coin = rand ( );
        if ( coin < 0.5 )
          s = s + 1;
        end
      end

      if ( s == 1 || s == player_num - 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);
  end
  average = average / trial_num;

  fprintf ( 1, '\n' );
  fprintf ( 1, '  The average number of flips for %d players is %g\n', ...
    player_num, 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 ( 'on' );
  filename = 'odd.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );

  return
end
