Skip to main content

weighted_index

Function weighted_index 

Source
pub fn weighted_index(weights: &[FloatType], draw: FloatType) -> Result<usize>
Expand description

Which of the weighted choices a draw draw from the unit interval lands on, as music21’s weightedSelection picks one: the last where it lands on none.

use music21::common::numbertools::weighted_index;

assert_eq!(weighted_index(&[1.0, 3.0], 0.2)?, 0);
assert_eq!(weighted_index(&[1.0, 3.0], 0.3)?, 1);

§Errors

A negative weight.