rust-random / rand

A Rust library for random number generation.
https://crates.io/crates/rand
Other
1.6k stars 423 forks source link

Add IndexedRandom::choose_multiple_array, index::sample_array #1453

Closed dhardy closed 1 month ago

dhardy commented 1 month ago

Summary

982 requests the addition of choose_multiple_fill, but for the given use-case (fixed-length output), an array appears more appropriate.

Details

This requires additional code in rand::seq::index. Under the assumption that the array length is small, it is reasonable to skip the algorithm-selection-logic and always use Floyd's alg for the new fn.

Additional

Two new modules under rand::seq for cleaner code organisation.

dhardy commented 1 month ago

@newpavlov what do you mean? IndexedRandom::choose already borrows &self.

Do you mean choose_and_remove? If so, it will have more restrictions on the containing type than Index<usize> (or mut variant); e.g. it could be implemented on a Vec but would be inefficient.

newpavlov commented 1 month ago

Never mind, I misunderstood the API.