Artificial Wasteland, a layer of the ground

The One the River Keeps

algorithms  ·  probability  ·  a fair winner from a line whose end you never see, holding only one at a time

Members of a crowd file past you one at a time. You may hold exactly one. You are never told how many will come, and once someone has passed you cannot call them back. When the last one goes by, the person in your hand must have had the same chance as everyone else. It sounds impossible. One rule does it.

I · A line with no end in sight

Here is the honest shape of the problem. A file of items comes at you in order, and you get to see each one only as it passes. You have room for a single keeper. Crucially, you do not know the length of the line in advance, so you cannot say "I will keep the 47th" or roll a die over a count you do not have. When the stream finally stops, the item you are holding should be a uniformly random one, as if you had laid the whole crowd out and pointed blind.

The rule that achieves it, called Algorithm R, is almost too short to trust. Keep the first item. Then, when the i-th item arrives, let it knock out whoever you are holding with probability 1/i. The second arrival replaces the first half the time; the third takes over a third of the time; the hundredth barges in only once in a hundred. Try it. Step the stream one at a time and watch who survives, or let it run.

Instrument I · operate the reservoir (you are never told n)
length n 16
The arriving item's odds of taking the slot are 1 / i, and they shrink as the line grows, yet (next section) every position still finishes with the same chance. The slot never holds more than one thing; the line's length is revealed only when it ends.

II · Why fading odds come out even

The surprise is that a rule whose odds visibly decay does not favor the early arrivals (who had many chances to be evicted) nor the late ones (who barely had a chance to enter). It is exactly flat. You can prove it in one line, and it is worth doing by hand because the cancellation is the whole magic.

Fix any position j. For item j to be the final survivor, two things must happen: it must take the slot when it arrives (probability 1/j), and then every later arrival must decline to evict it. Arrival i > j leaves it alone with probability 1 − 1/i = (i−1)/i. Multiply:

P(item j wins) = (1/j) · (j/(j+1)) · ((j+1)/(j+2)) · … · ((n−1)/n)

Every numerator cancels the next denominator. The j in front cancels the j that opens the product; the whole chain collapses to

P(item j wins) = (1/j) · (j/n) = 1/n.

No j left in the answer: the position dropped out. Whoever you are holding at the end had probability exactly 1/n, and the algorithm never once needed to know n while it ran. Below, run the whole thing many times over and watch the survivor counts settle onto the flat line 1/n. This is the proof made of tallies rather than algebra.

Instrument II · ten thousand runs, one flat line
n 20
Each bar is how often that position ended up the survivor. The dashed line is the predicted 1/n. The chi-square statistic measures the gap from flat; with n−1 degrees of freedom it should sit comfortably below its 0.5% critical value, and it does: the histogram is uniform, not merely close.

III · The rule that feels fair, and isn't

The 1/i schedule is not decoration. It is the only schedule that flattens. To feel why, try the rule almost everyone reaches for first: at each step, replace what you hold with some fixed probability p. It looks even-handed, symmetric, no favoritism. It is a disaster. The very last item wins whenever it chooses to replace, which is with probability p, no matter how long the line was. The second-to-last wins with probability p(1−p), and so on: the chances fall off geometrically from the end, so recent arrivals dominate and the front of the line is all but forgotten. Slide p and watch the skew; the reservoir's 1/i rule is drawn beside it for contrast.

Instrument III · fixed-p (tempting, wrong) vs. 1/i (fair)
fixed p 0.50
fixed probability p  ·  skews to the end
reservoir 1/i  ·  flat
Same stream (n = 12), same number of runs, two rules. The left rule's last bar sits near p; the right rule's bars sit near 1/12. Symmetry of the coin is not the same thing as fairness of the outcome.

IV · More than one, and not all equal

The same idea scales. To keep a fair sample of k items instead of one, fill the reservoir with the first k arrivals; then, when item i > k comes, admit it with probability k/i, and if admitted let it replace one of the k held items chosen at random. The same telescoping argument now gives every item probability exactly k/n of being in the final sample, using memory for only k items however long the river runs.

Instrument IV · a fair sample of k (each item retained k/n of the time)
n 24 k 4
Every bar tracks how often that item survived into the sample of k. The dashed line is k/n. Flat again: no position is favored, and the reservoir only ever holds k things.

And what if the items are not equal? Suppose each carries a weight, and you want heavier items to appear proportionally more often. Efraimidis and Spirakis found a rule as clean as the original (2006). Give item i a random key ui1/wi, where ui is uniform on (0,1) and wi is its weight; keep the item with the largest key. For a single keeper the guarantee is exact and beautiful: item i wins with probability wi / Σw. The proof is one integral. The key of item i lands below x with probability xwi, so the chance it beats all the others is

∫₀¹ w_i · x^(w_i − 1) · Π_(j≠i) x^(w_j) dx = ∫₀¹ w_i · x^(Σw − 1) dx = w_i / Σw.

Set the four weights below and run: the empirical win-rates climb to wi/Σw. (Equal weights collapse it back to the plain uniform reservoir, as it must.)

Instrument V · weighted: item i wins in proportion to its weight
Solid bars are the measured win-rate; the notch on each is the target wi/Σw. This exact form holds for a single keeper; sampling several weighted items at once is a subtler object (see the notes below).

V · Where this actually runs

This is not a museum piece. The moment a system must sample from something it cannot fit in memory or whose size it does not know until the end, reservoir sampling is the tool. Think of a firehose of log lines or telemetry events arriving faster than you can store them: to keep a uniform sample of, say, a thousand for later analysis, you run Algorithm R with k = 1000 and touch each event exactly once, holding a fixed thousand no matter whether a million or a billion go by. You have used it without knowing: the Unix tool shuf, asked for shuf -n 1000 lines of a file, switches to a reservoir once the input grows past a few megabytes, so it keeps just those thousand lines in memory instead of loading the whole file (this is right there in coreutils' shuf.c, with its comment on choosing "the fate of the next line, with decreasing probability"). The same one-pass, bounded-memory shape recurs across databases, big-data pipelines, and streaming frameworks whenever a representative subsample is wanted in a single pass.

Its lineage is old, and worth telling straight because the tidy single-inventor version is a myth. The single-item rule appears as Algorithm R in Knuth's The Art of Computer Programming, credited there to Alan Waterman, who by Knuth's own account never published it himself (it was later rediscovered independently, so there is no clean "first"). In 1985 Jeffrey Vitter popularized the name reservoir sampling and added a decisive speed-up. His method still reads every arrival, but instead of flipping a coin at each one it computes in a single draw how many arrivals to skip before the next replacement, cutting the number of random draws (and the CPU work) from proportional to n down to about k(1 + log(n/k)). The sample it produces is identical in distribution; only the labor changes. (Reservoir sampling is never sublinear in the stream: any correct method must see every item go by.)

VI · What is proven, and what is modelled

The core claims here are theorems and are exact: the single-item rule gives every position probability 1/n (a telescoping product, computed in exact rationals in the verifier for n up to 100); the general rule gives every item k/n; the fixed-probability rule is provably geometric toward the end, not uniform; and the weighted single-keeper rule gives wi/Σw (an integral, checked three ways). Every histogram on this page is a live Monte-Carlo simulation with a fixed seed, and the same simulations run offline pass a chi-square uniformity test and match the exact formulas to within sampling error.

Two honest edges. First, a histogram converging toward flat is evidence of uniformity, not the proof of it; the proof is the algebra above, and the tallies are there so you can watch the theorem hold rather than take it on faith. Second, the weighted rule's clean wi/Σw statement is for a single survivor. Taking a weighted sample of several items at once with this key method (A-Res) draws them without replacement, item by item in proportion to remaining weight; the marginal probability that a given item lands in a size-k weighted sample is genuinely useful but is not simply k · wi/Σw, and this page does not claim it is.

Where this connects on the ground