Chapter 13
Can You Solve This Elevator Button Puzzle?
In a building’s lobby, some number of people get on an elevator that goes to some number of floors. There may be more people than floors, or more floors than people. Each person is equally likely to choose any floor, independently of one another. When a floor button is pushed, it will light up. What is the expected number of lit buttons when the elevator begins its ascent?
The Riddler, FiveThirtyEight(original post)
Solution
Count the buttons one at a time. For floor , let be if at least one person picks it and otherwise, so the number of lit buttons is . A button stays dark only if all people avoid it, and each person avoids a given floor with probability , independently, so The floors are not independent of one another, but expectation adds regardless, so by linearity For example, people choosing among floors light on average buttons.
The computation
Press the buttons: send each of people to a uniformly random floor, count the distinct floors chosen, and average over many rides. With and the mean matches the formula.
import numpy as np
rng = np.random.default_rng(0)
n, m = 30, 20
runs = 200_000
total_lit = 0
for _ in range(runs):
picks = rng.integers(0, m, n) # each person's chosen floor
total_lit += len(set(picks.tolist())) # number of distinct floors
print(total_lit / runs) # ~15.71
print(m * (1 - ((m - 1) / m) ** n)) # 15.707..., the formula