function ranking ( trial_num, m )

%*****************************************************************************80
%
%% ranking() simulates the result of guessing on a ranking test.
%
%  Discussion:
%
%    A list of M items is given.
%    The test taker is required to give a rank for each.
%    For each item, the test taker randomly chooses a value between 1 and M.
%    What is the average number of correct rankings?
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    27 January 2018
%
%  Author:
%
%    Paul Nahin
%
%  Reference:
%
%    Paul Nahin,
%    Digital Dice: Computational Solutions to Practical Probability Problems,
%    Princeton, 2008,
%    ISBN: 978-0-691-15821-1.
%
%  Input:
%
%    integer trial_num: the number of trials.
%
%    integer m, the number of items to be ranked.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'ranking():\n' );
  fprintf ( 1, '  A list of %d items is given.\n', m );
  fprintf ( 1, '  The test taker is required to give a rank for each.\n' );
  fprintf ( 1, '  For each item, the test taker randomly chooses a value between 1 and M.\n' );
  fprintf ( 1, '  What is the average number of correct rankings?\n' );

  total_correct = 0;

  for k = 1 : trial_num

    correct = 0;
    term = zeros ( 1, m );
    for j = 1 : m
      term(j) = ceil ( m * rand ( ) );
    end

    for j = 1 : m
      if ( term(j) == j )
        correct = correct + 1;
      end
    end

    total_correct = total_correct + correct;

  end

  total_correct = total_correct / trial_num;

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Average number of correct matches = %g\n', total_correct );

  return
end
