# Find Your Seat: Airplane Probability Problem

Python Puzzles

Back to the Python! homepage

You are running late in an airport and are in the very back of the line to board your plane. The plane seats fifty people. The first person in line forgot his seat number and chooses a seat at random when he enters the plane. Each subsequent person will sit in their assigned seat unless it is taken by someone else. If they find their seat already occupied, they will choose another seat at random.

If you are the last person to board the plane, what is the probability that you will sit in your assigned seat?

When I run the simulation 100,000 times, the probability of the last boarding passenger sitting in their assigned seat is 50%.

import random
simulations = 100_000

assigned_seat_results = []
while _ in range(simulations):

# create a list of 50 seats and 50 passengers
# passenger 1 will be assigned to seat 1,
# passenger 2 will be assigned to seat 2, and so on....
# passengers board in order of their passenger number
passengers = [i for i in range(1,51)]
seats = [i for i in range(1,51)]

# first passenger chooses a random seat
random_seat = (random.choice(seats))
seats.remove(random_seat)
i = 1

while seats:

# if the passenger seat is available, delete the seat from the list
if passengers[i] in seats:
# determine is it's the last passenger (passenger 50)
# if so, then seat 50 is available and append a1 to the results
if passengers[i] == max(passengers): assigned_seat_results.append(1)
seats.remove(passengers[i])
i = i + 1

# if the passenger seat is unavailable, select a random seat to delete
else:
random_seat = (random.choice(seats))
seats.remove(random_seat)

# determine if it's the last passenger (passenger 50)
# if so, then seat 50 is occupied (deleted) and
# append a 0 to the results
if passengers[i] == max(passengers): assigned_seat_results.append(0)
i = i + 1

percent_correct = (sum(assigned_seat_results)/simulations)
print('The probability is ' + '{0:.2%}'.format(percent_correct))

You can also use the following Python code to determine the outcome.

# Initialize the probability of the first passenger getting their own seat
p = 1/50

# Loop through the remaining passengers and calculate the probability of them getting their own seat
for i in range(2, 51):
p *= (i-2)/i

# Calculate the final probability by adding the probability of the first passenger getting their own seat
# and the probability of the last passenger getting their own seat
result = 1/2 + (49/50) * p

# Print the result
print(result)