function odd ( N )

%*****************************************************************************80
%
%% ODD simulates the game of odd man out.
%
%  Discussion:
%
%    This m-file 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...
%    
%  Modified:
%
%    29 January 2018
%
%  Author:
%
%    Paul Nahin
%
%  Reference:
%
%    Paul Nahin,
%    Dueling Idiots and Other Probability Puzzlers,
%    Princeton, 2012,
%    ISBN: 978-0691155005.
%
%  Input parameters:
%
%    Input, integer N, the number of players.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'ODD:\n' );
  fprintf ( 1, '  This m-file simulates 1,000 games of odd-person-out, for N people as\n' );
  fprintf ( 1, '  defined by the user, with each person flipping 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, where i=1,2,3...\n' );

  duration=zeros(1,50);

  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

    duration(flip) = duration(flip) + 1;

  end

  average = 0;
  for i = 1 : length ( duration )
    average = average + i * duration(i);
  end
  average = average / 1000;

  fprintf ( 1, '\n' );
  fprintf ( 1, '  The average number of flips for %d players is %g\n', ...
    N, average );
%
%  Graphics.
%
  clf
  bar ( duration )
  title('Fig.9.1-Relative Frequency of the Duration of Odd-Person-Out')
  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 );
  pause ( 5 );

  return
end
