function guess ( trial_num, m )

%*****************************************************************************80
%
%% guess() estimates the average result of randomly guessing ranks of M items.
%
%  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, 'guess():\n' );
  fprintf ( 1, '  Given M items of ranks 1 through M,\n' );
  fprintf ( 1, '  randomly guess the rank of each item. \n' );
  fprintf ( 1, '  On average, how many ranks will we guess correctly?\n' );

  total_correct = 0;

  for k = 1 : trial_num

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

  end

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Average number of correct pairings = %g\n', ...
    total_correct / trial_num );
  fprintf ( 1, '  Expected value is 1.\n' );

  return
end
