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

RFC: choose_weighted #253

Closed
Dowwie opened this issue Feb 5, 2018 · 1 comment
Closed

RFC: choose_weighted #253

Dowwie opened this issue Feb 5, 2018 · 1 comment

Comments

@Dowwie
Copy link

Dowwie commented Feb 5, 2018

Would you like to add a choose_weighted function to the library?

fn choose_weighted<'a, T>(&mut self, values: &'a [T], weights: &[f32]) -> Option<&'a T> where
    Self: Sized, 
[−]

Return a weighted-random element from values.

Return None if values is empty.
Example

use rand::{thread_rng, Rng};

let choices = [20, 30, 40, 10];
let weights = [.60, 20, 15, 5]; 
let mut rng = thread_rng();
println!("{:?}", rng.choose_weighted(&choices, &weights));
@dhardy
Copy link
Member

dhardy commented Feb 6, 2018

We already have WeightedChoice which does roughly this, though I'm tempted to remove the items ("choices") themselves and just return an index.

See also dhardy#82

@Dowwie Dowwie closed this as completed Feb 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants