Skip to content
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

Consider adding Rng::choose #1166

Open
makoConstruct opened this issue Aug 24, 2021 · 3 comments
Open

Consider adding Rng::choose #1166

makoConstruct opened this issue Aug 24, 2021 · 3 comments
Labels
C-docs Documentation E-question Participation: opinions wanted

Comments

@makoConstruct
Copy link

It seems that the current way to sample from a slice is s[rng.gen_range(0..s.len())]. This is not as nice as rng.sample(s) would be, but for some reason Distribution is not implemented for &[T]?

@makoConstruct
Copy link
Author

makoConstruct commented Aug 24, 2021

I have found out about SliceRandom, but this functionality really should be discoverable in the same box as the rest of the Distribution sampling stuff rather than being hidden away in a separate trait.

@vks
Copy link
Collaborator

vks commented Aug 24, 2021

Distribution is already implemented for slices. Note that it is not implemented for &[T], because it requires preprocessing.

SliceRandom has to be its own trait, because "choosing without replacement" is not giving independent samples, and is therefore not supported by Distribution.

However, we could add choose as a convenience methods to Rng.

@vks vks changed the title Implement Distribution for slices Consider adding Rng::choose Sep 7, 2021
@dhardy
Copy link
Member

dhardy commented Jul 20, 2024

We could add Rng::choose (supporting only slices), but it's duplicate functionality as is seen by a simple search. It also couldn't support both iterators (which need to take self by value) and slices (for which the desired receiver is normally &self).

We could implement Distribution<T> for [T], but not a mutable variant. Also, Distribution objects are usually used where it is desired to pay the set-up cost for more efficient sampling later.

Hence, I'd prefer to solve this with only documentation; the Getting Started guide should likely mention this.

@dhardy dhardy added C-docs Documentation E-question Participation: opinions wanted labels Jul 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-docs Documentation E-question Participation: opinions wanted
Projects
None yet
Development

No branches or pull requests

3 participants