Implement reservoir_sample(stream, k, seed) that selects at most k items from a single pass over stream (any iterable, possibly of unknown length) so that every item seen is equally likely to be in the result. Use only extra memory; never materialize the whole stream.
Use Algorithm R with rng = random.Random(seed) as the only source of randomness:
j = rng.randint(0, i); if , the item replaces reservoir[j], otherwise it is discarded.Return the reservoir as a list. If the stream has fewer than k items, this is simply all of them in order.