On this page
Imagine names arriving one by one from a queue at an exam centre, and you must choose 2 of them fairly without knowing how long the queue is or writing every name down. Reservoir sampling keeps a small "reservoir" of k items and updates it as each item arrives, so that when the stream stops, every item has had the same chance.
What is reservoir sampling?
Reservoir sampling selects a random sample of k items without replacement, in one pass, from a stream whose length n is not known in advance. Jeffrey Vitter's 1985 paper Random sampling with a reservoir set out the problem this way. The memory needed is O(k), the size of the reservoir, however long the stream runs (Big O notation, explained).
How does Algorithm R work, step by step?
The simplest method is Algorithm R. The Wikipedia article on reservoir sampling credits it to Alan G. Waterman, citing Knuth's The Art of Computer Programming, volume 2, and notes that McLeod and Bellhouse found it independently.
Put the first k items into the reservoir, slots 1 to k.
For each later item i (i = k+1, k+2, ...), draw a random integer j uniformly from 1 to i.
If j is at most k, replace slot j with item i. Otherwise, throw item i away.
When the stream ends, the reservoir is your sample.
So item i enters with probability k/i and evicts a uniformly chosen member. Algorithm R draws one random number per item, so it runs in linear time; Vitter's Algorithm Z skips ahead and runs in O(k(1 + log(n/k))) expected time, optimal up to a constant factor according to the 1985 paper.
Why does every item end up with probability k/n?
Claim: after the first t items (t at least k), each of them is in the reservoir with probability k/t.
Base case, t = k. All k items are in the reservoir, so each has probability k/k = 1.
Inductive step. Suppose the claim holds after t items. Item t+1 enters with probability k/(t+1), which is what we want for it. An older item is removed only if item t+1 enters and lands on its slot: probability k/(t+1) times 1/k, which is 1/(t+1). So it survives with probability t/(t+1), and its new chance is (k/t) times t/(t+1) = k/(t+1).
By induction, after all n items every item is in the sample with probability k/n. Directly, for a late item (i greater than k): it enters with probability k/i, then survives with (i/(i+1))((i+1)/(i+2))...((n-1)/n) = i/n, and k/i times i/n = k/n.
If induction or conditional probability feels shaky, the Fermi Concept Library gives each concept its own lesson and its place in the map.
Worked example: a stream of 6 items with k = 2
Stream: A, B, C, D, E, F, with k = 2. One run, draws shown:
A and B fill the reservoir: [A, B].
C (i = 3): j = 2, at most 2, so C replaces slot 2: [A, C].
D (i = 4): j = 4, discarded: [A, C].
E (i = 5): j = 1, E replaces slot 1: [E, C].
F (i = 6): j = 5, discarded. Final sample: [E, C].
Now the probabilities, not just this run:
A (in from the start) survives steps 3 to 6: (2/3)(3/4)(4/5)(5/6) = 2/6 = 1/3.
C enters with 2/3, then survives steps 4 to 6: (3/4)(4/5)(5/6) = 1/2. Total 2/3 times 1/2 = 1/3.
E enters with 2/5, survives step 6 with 5/6. Total 2/6 = 1/3.
F enters with 2/6 = 1/3 and nothing comes after it.
Every item finishes at 1/3 = k/n. Try B and D yourself.
How do you do weighted reservoir sampling?
Sometimes items should count unequally: a question you got wrong three times deserves more revision draws than one you got wrong once. Pavlos Efraimidis and Paul Spirakis gave a one-pass method in Weighted random sampling with a reservoir (Information Processing Letters, 2006). Their algorithm A-Res, as written out in the Wikipedia article, gives an item with weight w the key u^(1/w), with u uniform on (0, 1), and keeps the k largest keys in a min-heap. A heavier weight pushes the key towards 1, so heavy items stay more often. The same authors also gave a faster version, A-ExpJ.
For other small-memory and sampling tricks, see rejection sampling and the Bloom filter false positive rate.
What mistakes break the uniform sample?
Drawing j from 1 to k instead of 1 to i: every new item enters and late items dominate.
An off-by-one, such as drawing from 0 to i, which slightly biases the sample.
Evicting "the oldest" member instead of slot j.
If a step in the proof or your own code does not add up, the Fermi Problem Library has worked problems with a step-by-step solver that explains the reasoning rather than printing an answer.



