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))
    i = 1

    while seats:
        #if the passenger seat is availble, 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 avaialble and append a1 to the results
            if passengers[i] == max(passengers): assigned_seat_results.append(1)   
            i = i + 1
        #if the passenger seat is unavailable, select a random seat to delete
            random_seat = (random.choice(seats))

            #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))