-
Notifications
You must be signed in to change notification settings - Fork 432
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add choose_multiple_fill for SliceRandom #982
Comments
Rejection sampling is a very good approach in this case ( All these have something in common: they need some buffer to store selected indices in. We can't directly use the output buffer, since we need to avoid sampling any index multiple times but multiple copies of an output value may be valid (if appearing in your slice multiple times). So the best we could do without allocating is: pub fn sample_buf<R>(rng: &mut R, length: usize, buf: &mut [usize])
where R: Rng + ?Sized { ... } Unfortunately, we'd have to duplicate all the methods in Note further: |
This ("sample exactly 2 members of a slice") was implemented in #1453. |
Background
Currently
IteratorRandom
provides an allocation free way to sample members from it withchoose_multiple_fill
, problem is it'sO(n)
over the size of the iterator. This makes sense but then the docs point toSliceRandom::choose_multiple
which isO(amount)
but it always allocates a new vec of indexes to back the iterator it returns.I need to sample exactly 2 members of a slice in a loop, it would be nice to have a way to avoid allocating in a loop that doesn't need to as this will be a major performance hit.
Right now I have to do
which I doubt is a good way to achieve randomness.
Feature request
Add to SliceRandom
I can try to write a PR for this if it seems feasible but I don't have experience with rng, I would look at the rest of the implementations, specially
SliceRandom::choose_multiple
for inspiration.The text was updated successfully, but these errors were encountered: