Jump to text
On this page
Copy and source
Published source confirmed at last check

Source changed 2026-11-26 21:07:53 UTC · snapshot created 2026-11-26 21:08:36 UTC · last check 2026-11-29 22:35:20 UTC

CMP.9:5.1 - Keep one uniform record from a stream of unknown length

Store the first record. At record t, use a fresh uniform integer in 1 through t to replace the stored record with probability 1/t. The guarantee below concerns each fixed input prefix. It also applies conditional on a completed stream when the input and its endpoint are independent of the sampler’s random choices.

After t records, the new record has probability 1/t. Every earlier record had probability 1/(t-1) before this step and survives with probability (t-1)/t, giving 1/t as well. This establishes the invariant inductively for each fixed prefix. If a sender ends a stream of distinct records A, B immediately after the second record whenever A is retained, and otherwise sends C, every observed stream of length two ends with A retained. Uniformity at a fixed prefix alone cannot establish uniformity conditional on this stopping event. A different stopping policy can preserve uniformity if its effect is accounted for. The procedure needs one record, a counter and one update decision per arrival. Counter and record representation costs remain visible.

For the stream A, A, B, the resulting value is A with probability 2/3 and B with probability 1/3. This is correct uniform sampling of record positions. If the new request is uniform sampling of distinct values, the old invariant answers a different question. One construction keeps a set of already seen values and applies the same update only on a first occurrence. That requires storage for the distinct-value set or another algorithm with its own access and error assumptions.