Function rv::misc::ln_pflip[][src]

pub fn ln_pflip<R: Rng>(
    ln_weights: &[f64],
    n: usize,
    normed: bool,
    rng: &mut R
) -> Vec<usize>

Draw an index according to log-domain weights

Draw a usize from the categorical distribution defined by ln_weights. If normed is true then exp(ln_weights) is assumed to sum to 1.

Examples

use rv::misc::ln_pflip;

let weights: Vec<f64> = vec![0.4, 0.2, 0.3, 0.1];
let ln_weights: Vec<f64> = weights.iter().map(|&w| w.ln()).collect();

let xs = ln_pflip(&ln_weights, 100, true, &mut rand::thread_rng());

assert_eq!(xs.len(), 100);
assert!(xs.iter().all(|&x| x <= 3));
assert!(!xs.iter().any(|&x| x > 3));