[][src]Function mc_sim::stats::attempts_to_reach_target

pub fn attempts_to_reach_target(min: i32, max: i32, target: i32) -> f64

Answers the question "how many dice do I need to roll to get to a target"? Implementation based on the answer by Varun Vejalla: https://math.stackexchange.com/a/3965269/867664

assert_eq!(round(stats::attempts_to_reach_target(1, 6, 1), 4), 1.0000);
assert_eq!(round(stats::attempts_to_reach_target(1, 6, 4), 4), 1.5880);
assert_eq!(round(stats::attempts_to_reach_target(1, 6, 30), 4), 9.0476);
assert_eq!(round(stats::attempts_to_reach_target(1, 6, 36), 4), 10.7619);
assert_eq!(round(stats::attempts_to_reach_target(1, 6, 80), 4), 23.3333);

let drop_list = drop_list::barter_drop_list(10, 10);
let drop_range = stats::item_drop_range(drop_list.list(), Item::EnderPearl);
assert_eq!(
    round(
        stats::attempts_to_reach_target(drop_range.0 as i32, drop_range.1 as i32, 10),
        4
    ),
    2.1200
);

fn round(f: f64, p: u32) -> f64 {
    let precision = (10.0 as f64).powf(p as f64);
    (f * precision).round() / precision
}