function test ( M )

%*****************************************************************************80
%
%% TEST 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?
%
%  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 parameters:
%
%    Input, integer M, the number of items to be ranked.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'TEST:\n' );
  fprintf ( 1, '  A list of M items is given.\n' );
  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 : 1000000

    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 / 1000000;

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

  return
end
