Math Fundamentals18 sections · 814 units
Open in Course

Pick Index - Reservoir Sampling

Fair selection in one pass

Reservoir sampling lets you pick one element uniformly from a stream without knowing the count beforehand.

Here's the algorithm: keep the first matching element. When you find the second match, replace your choice with probability 12\frac{1}{2}. For the third match, replace with probability 13\frac{1}{3}, and so on.

Why does this work? After seeing kk matches, your current choice has survived kk replacement tests, each with decreasing probability. The math shows each index has final probability 1k\frac{1}{k}.