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 . For the third match, replace with probability , and so on.
Why does this work? After seeing matches, your current choice has survived replacement tests, each with decreasing probability. The math shows each index has final probability .