Skip to content

Latest commit

History

98 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Rust Random Choice

Chooses samples randomly by their weights/probabilities.

Advantages

  • There is a good diversity for the case that all weights are equally distributed (in contrast to the roulette wheel selection algorithm)
  • Blazingly fast: O(n) (Roulette wheel selection algorithm: O(n * log n))
  • Memory Usage: O(n)
  • The sum of the weights don't have to be 1.0, but must not overflow

This algorithm is based on the stochastic universal sampling algorithm.

Applications

  • Evolutionary algorithms: Choose the n fittest populations by their fitness fi
  • Monte Carlo Localization: Resampling of n particles by their weight w

Usage

Add this to your Cargo.toml:

[dependencies]
random_choice = "*"

Examples

Default Way

externcrate random_choice;useself::random_choice::random_choice;fnmain(){letmut samples = vec!["hi","this","is","a","test!"];let weights:Vec<f64> = vec![5.6,7.8,9.7,1.1,2.0];let number_choices = 100;let choices = random_choice().random_choice_f64(&samples,&weights, number_choices);for choice in choices {print!("{}, ", choice);}}

With Custom Seed

externcrate rand;externcrate random_choice;use random_choice::RandomChoice;use rand::SeedableRng;fnmain(){letmut samples = vec!["hi","this","is","a","test!"];let weights:Vec<f64> = vec![5.6,7.8,9.7,1.1,2.0];let rng = rand::StdRng::from_seed(&[5000,44,55,199]);letmut random_choice = RandomChoice::new(rng);let number_choices = 100;let choices = random_choice.random_choice_f64(&mut samples,&weights, number_choices);for choice in choices {print!("{}, ", choice);}}

About

Chooses samples randomly by their weights/probabilities

Resources

Stars

13 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages