← All Tools

๐ŸŽฐ Reservoir Sampling Visualizer

Watch Algorithm R (Vitter, 1985) and Algorithm L pick k uniformly random items from a stream of unknown length. Each item ends up in the reservoir with probability exactly k/n โ€” verify it with a Monte Carlo run.

โš™๏ธ Setup

Items flowing past, drawn one at a time.
How many to keep. Must be โ‰ค n.
L skips ahead in O(k(1+log(n/k))) steps.
0 = instant. Use 0 for big runs.
Mulberry32 PRNG for reproducible runs.
i (Items Seen)
0
In Reservoir
0 / 0
Last Decision
โ€”
Accept Probability
โ€”
Replacements
0

๐ŸŒŠ Stream

๐Ÿบ Reservoir

๐Ÿ“œ Log

๐Ÿ“ˆ Uniformity Test

After a Monte Carlo run, every item j โˆˆ [1, n] should appear in the reservoir k/n of the time. The bars below show empirical inclusion frequency per stream position. A flat line at k/n means the algorithm is unbiased.

Run a Monte Carlo to populate.

How It Works

Algorithm R โ€” fill the reservoir with the first k items. For each subsequent item i (1-indexed, i > k), pick an integer j โˆˆ [1, i] uniformly. If j โ‰ค k, replace slot j. This gives each item probability k/n of being in the final sample.

Algorithm L โ€” instead of testing every item, draw a geometric "skip" count floor(log(random()) / log(1-W)) where W starts at 1 and gets multiplied by exp(log(random())/k) after each replacement. Same uniformity guarantee, far fewer random draws on huge streams.