MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / uniform_distribution

Function uniform_distribution

nodedb/src/engine/random/alias.rs:157–176  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

155
156 #[test]
157 fn uniform_distribution() {
158 let weights = vec![1.0, 1.0, 1.0, 1.0];
159 let table = AliasTable::new(&weights).unwrap();
160 assert_eq!(table.len(), 4);
161
162 let mut rng = SeedableRng::from_seed_str("uniform-test");
163 let mut counts = [0u32; 4];
164 let n = 10_000;
165 for _ in 0..n {
166 counts[table.sample(&mut rng)] += 1;
167 }
168
169 // Each should be roughly 2500 ± 300.
170 for (i, &c) in counts.iter().enumerate() {
171 assert!(
172 c > 2000 && c < 3000,
173 "item {i} sampled {c} times, expected ~2500"
174 );
175 }
176 }
177
178 #[test]
179 fn skewed_distribution() {

Callers

nothing calls this directly

Calls 2

sampleMethod · 0.80
iterMethod · 0.45

Tested by

no test coverage detected