Expand description
Sampling routines whose results depend only on the bytes an RNG produces,
never on which version of rand is in the dependency graph.
rand’s own shuffle, choose, choose_multiple and
choose_multiple_weighted are free to change between releases, and they
have: 0.9 replaced the range sampler and the weighted-selection wrapper, so
the same seed produces a different permutation. That is fine for a test
fixture and unacceptable for a permutation every validator has to agree on.
The routines here reproduce rand 0.8’s algorithms and are fixed by
tests against captured vectors, so callers that need a permutation to
stay the same forever can bump rand freely.
Two rules for callers:
- Seed [
rand::rngs::ChaCha12Rng], notStdRng.StdRngis documented as non-portable —randreserves the right to swap the algorithm under it — whereasChaCha12Rngnames the generator and is whatStdRngcurrently happens to be. - Draw floats through
f64_unitrather thanrand’srandom::<f64>(), which is another conversionrandowns and could change.
Functions§
- choose
- Returns a uniformly drawn element, or
Noneifsliceis empty. - f64_
unit - Draws a
f64uniformly from[0, 1), using the top 53 bits of one 64-bit word. - index
- Draws an index uniformly from
0..ubound. - sample_
indices - Draws
amountdistinct indices from0..length, in random order. - sample_
weighted - Draws
amountdistinct indices from0..lengthwith probability proportional to weight, in descending order of the keys drawn. - shuffle
- Permutes
slicein place (Fisher-Yates, from the back).