function airplane_seat ( m, n )

%*****************************************************************************80
%
%% AIRPLANE_SEAT simulates the airplane seat problem.
%
%  Discussion:
%
%    Airline passengers 1 through N are assigned seats 1 through N.
%    But passenger 1 is clueless, and chooses a seat randomly.
%    The remaining passengers board in order, choosing their assigned seat
%    if possible, otherwise taking an empty seat at random.
%    What is the probability that the last passenger, N, will be able to 
%    sit in the correct assigned seat N?
%
%  Modified:
%
%    30 January 2018
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Paul Nahin,
%    Will You Be Alive 10 Years From Now?,
%    Princeton, 2014,
%    ISBN: 978-0691156804,
%    LC: QA273.25.N344
%
%  Input parameters:
%
%    Input, integer M, the number of simulations.
%
%    Input, integer N, the number of passengers.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'AIRPLANE_SEAT:\n' );
  fprintf ( 1, '   Airline passengers 1 through N are assigned seats 1 through N.\n' );
  fprintf ( 1, '   But passenger 1 is clueless, and chooses a seat randomly.\n' );
  fprintf ( 1, '   The remaining passengers board in order, choosing their assigned seat\n' );
  fprintf ( 1, '   if possible, otherwise taking an empty seat at random.\n' );
  fprintf ( 1, '   What is the probability that the last passenger, N, will be able to \n' );
  fprintf ( 1, '   sit in the correct assigned seat N?\n' );

  prob = 0.0;

  for test = 1 : m

    occupant = zeros ( n, 1 );
%
%  Passenger 1 chooses a seat at random.
%
    passenger = 1;
    seat = randi ( [ 1, n ] );

    occupant(seat) = passenger;
%
%  Passenger I will choose seat I if it is available,
%  otherwise a random available seat.
%
    for passenger = 2 : n

      seat = passenger;
      while ( occupant(seat) ~= 0 )
        seat = randi ( [ 1, n ] );
      end

      occupant(seat) = passenger;

    end
%
%  Did passenger N sit in seat N?
%
    if ( occupant(n) == n )
      prob = prob + 1;
    end

  end

  prob = prob / m;
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Estimated probability   = %8.6g\n', prob );
  fprintf ( 1, '  Theoretical probability = %8.6g\n', 0.5 );

  return
end
