Skip to main content

Module sample

Module sample 

Source
Expand description

Reproducible random draws.

Two places already needed one and each wrote its own: the mail corpus grader, and the graph’s queue sampler one repository over. This is the third caller — prioritised replay’s holdout — and a third copy of a shuffle is a third place for the same bias to hide.

The seed is the caller’s and gets printed. A sample nobody can redraw is a sample nobody can check, and these exist to produce numbers somebody will quote.

Sort before you shuffle, or the seed is a lie. The mail grader learned this the expensive way: it shuffled a HashMap’s iteration order, which is randomised per process, so two runs with the same seed graded different samples while the flag documented itself as making a scorecard reproducible. A deterministic shuffle of a nondeterministic order is nondeterministic. Callers pass a slice whose order they control.

The PRNG is a four-line LCG rather than a dependency: nothing here needs cryptographic randomness, and rand would be a new dependency in a crate that reads the owner’s transcripts.

Functions§

shuffled
Fisher–Yates, seeded.
take_uniform
A uniform draw of at most k, reproducible from seed.