macro_rules! rand_weighted_choice {
    ($rng:expr, $choices:expr, $weights:expr) => { ... };
}
Expand description

Selects a random element from a slice based on the provided weights.

The weights determine the probability of each element being selected. The probability of an element being selected is proportional to its weight relative to the sum of all weights.

§Examples

use vrd::rand_weighted_choice;
let mut rng = vrd::random::Random::new();
let choices = ["A", "B", "C"];
let weights = [2, 3, 5];
let selected = rand_weighted_choice!(rng, &choices, &weights);
println!("Selected element: {}", selected);

§Arguments

  • rng - A mutable reference to a Random instance.
  • choices - A reference to the slice of elements to choose from.
  • weights - A reference to the slice of weights corresponding to each element.

§Panics

Panics if choices and weights have different lengths.

§Returns

A reference to the randomly selected element from choices.