A stream flows in one item at a time. Fill k slots, then keep the i-th item with probability k/i, evicting a uniformly random slot. When the stream ends, each item has probability k/n of being in the reservoir.
| Operation | Time | Space |
|---|---|---|
| Process one item | O(1) | — |
| Whole stream (Algo R) | O(n) | O(k) |
| RNG calls (Algo R) | O(n) | — |
| RNG calls (Algo L) | O(k·log(n/k)) | O(k) |
| Naive shuffle | O(n) | O(n) |
junior.md for the walkthrough and professional.md for the uniformity proof.