1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright © 2023-2026 vrd. All rights reserved.
// SPDX-License-Identifier: Apache-2.0 OR MIT
//! Weighted sampling via the `rand_weighted_choice!` macro.
//!
//! Run: `cargo run --example weighted`
#[path = "support.rs"]
mod support;
use vrd::{rand_weighted_choice, Random};
fn main() {
support::header("vrd -- weighted");
support::task_with_output(
"Weighted picks converge to the weights",
|| {
let mut rng = Random::from_u64_seed(0xCAFE);
let labels = ["common", "uncommon", "rare", "legendary"];
let weights: [u32; 4] = [60, 25, 10, 5];
let n = 20_000usize;
let mut counts = [0u32; 4];
for _ in 0..n {
let pick: &str =
rand_weighted_choice!(rng, &labels, &weights);
let i = labels
.iter()
.position(|x| *x == pick)
.expect("known label");
counts[i] += 1;
}
counts
.iter()
.enumerate()
.map(|(i, &c)| {
let observed = c as f64 / n as f64 * 100.0;
format!(
"{:<10} expected {:>3}% observed {:>5.2}%",
labels[i], weights[i], observed
)
})
.collect()
},
);
support::summary(1);
}