SSampleThicket
Workflows

Coding workflows · Stream reservoir sampling

03
Built around your work

A concrete starting point

Select a reservoir of at most 32 items from a finite stream of at most 1000 items using a local seeded generator. Repeat the seed to check reproducibility and reject a stream shorter than the sample, leaving input lists unchanged.

01

Open the context

Choose the project folder and name the files or behavior involved.

02

Define the change

Describe the input, expected output and boundaries that must stay intact.

03

Inspect the work

Review the proposed edits and any commands before relying on their result.

04

Check the outcome

Compare actual behavior with the expected result. If a check fails, investigate it before applying more changes.

Stream reservoir sampling

A concrete starting point
from random import Random

def reservoir_sample(items, size, seed):
    if type(size) is not int or not 0 <= size <= 32:
        raise ValueError("size.range")
    if type(seed) is not int or not 0 <= seed < 2**32:
        raise ValueError("seed.range")
    rng, sample, count = Random(seed), [], 0
    for index, item in enumerate(items):
        count = index + 1
        if count > 1000:
            raise ValueError("stream.limit")
        if index < size:
            sample.append(item)
        elif size:
            position = rng.randrange(count)
            if position < size:
                sample[position] = item
    if size > count:
        raise ValueError("stream.short")
    return sample

The example is a starting point for a real project, not a claim that generated code is already tested. Adjust paths, dependencies and expected behavior to match your application.

AI coding workspace

Your next change starts here.

Get the Windows app ↗