Skip to content

Commit d645952

Browse files
committed
Test and fix choose_multiple_weighted with very small probabilities: #1476
Also improves choose_two_weighted_indexed time by 23% (excluding new test)
1 parent 554d331 commit d645952

File tree

2 files changed

+2
-1
lines changed

2 files changed

+2
-1
lines changed

distr_test/tests/weighted.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ fn choose_two_weighted_indexed() {
181181
test_weights(100, |i| i as f64);
182182
test_weights(100, |i| (i as f64).powi(3));
183183
test_weights(100, |i| 1.0 / ((i + 1) as f64));
184+
test_weights(10, |i| ((i + 1) as f64).powi(-8));
184185
}
185186

186187
#[test]

src/seq/index.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ where
392392
while index < length {
393393
let weight = weight(index.as_usize()).into();
394394
if weight > 0.0 {
395-
let key = rng.random::<f64>().powf(1.0 / weight);
395+
let key = rng.random::<f64>().ln() / weight;
396396
candidates.push(Element { index, key });
397397
} else if !(weight >= 0.0) {
398398
return Err(WeightError::InvalidWeight);

0 commit comments

Comments
 (0)