Books · The Fiddler: Solutions
Chapter 91
Can You Find a Matching Pair of Socks (Again)?
Your drawer holds five distinct pairs of socks, ten socks in all, none paired up, and the room is pitch black. You pull socks out one at a time at random, and you want to stop as soon as you hold a matching pair. If you decide in advance how many socks to draw, how many should that be to maximise the chance that the last sock you draw is the one that first completes a pair?
The Fiddler, Zach Wissner-Gross, June 28, 2024(original post)
Solution
Drawing socks succeeds when the first are all different colours and the -th matches one of them. The chance the first are all distinct shrinks as grows, while the chance the next sock then finds a match grows, so the product peaks in the middle. For five pairs, giving for . The largest is at , so you should draw socks; the fourth completes the first pair with probability , the best of any choice.
The computation
Play it out: draw the ten socks in a random order, find the position at which a colour first repeats, and tally how often that position is each . The peak of the tally is at , with frequency .
import numpy as np
rng = np.random.default_rng(0)
socks = np.repeat(np.arange(5), 2) # five colours, two each
counts = np.zeros(11); trials = 1_000_000
for _ in range(trials):
perm = socks[rng.permutation(10)]
seen = set()
for i, col in enumerate(perm, start=1):
if col in seen: counts[i] += 1; break # first completed pair
seen.add(col)
P = counts / trials
print(int(P.argmax()), round(P[4], 3)) # 4 0.286
Extra Credit
With pairs and very large, how many socks should you draw?
Solution
The same expression governs it for pairs: climbs and then falls, and its peak drifts upward with exactly like a birthday-problem threshold. The first matching pair tends to arrive after about draws, the familiar square-root scaling of collisions, so the optimal number to commit to is The ratio falls steadily toward as grows: it is at , at , and at .
The computation
Evaluate the exact probability of completing the first pair on draw and maximise it over ; the optimum tracks .
import math
from fractions import Fraction as F
def P(k, N): # P(first pair completes exactly on draw k)
dist = F(1)
for i in range(k - 1): dist *= F(2 * (N - i), 2 * N - i) # first k-1 distinct colours
return dist * F(k - 1, 2 * N - (k - 1)) # k-th matches one of them
for N in (100, 500, 1000):
k = max(range(2, 2 * N + 1), key=lambda k: P(k, N))
print(N, k, round(k / math.sqrt(N), 3)) # k* ~ sqrt(2N)