Skip to content

Commit

Permalink
Test and fix choose_multiple_weighted with very small probabilities: r…
Browse files Browse the repository at this point in the history
…ust-random#1476

Also improves choose_two_weighted_indexed time by 23% (excluding new test)
  • Loading branch information
dhardy committed Nov 19, 2024
1 parent 554d331 commit d645952
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 1 deletion.
1 change: 1 addition & 0 deletions distr_test/tests/weighted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ fn choose_two_weighted_indexed() {
test_weights(100, |i| i as f64);
test_weights(100, |i| (i as f64).powi(3));
test_weights(100, |i| 1.0 / ((i + 1) as f64));
test_weights(10, |i| ((i + 1) as f64).powi(-8));
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/seq/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ where
while index < length {
let weight = weight(index.as_usize()).into();
if weight > 0.0 {
let key = rng.random::<f64>().powf(1.0 / weight);
let key = rng.random::<f64>().ln() / weight;
candidates.push(Element { index, key });
} else if !(weight >= 0.0) {
return Err(WeightError::InvalidWeight);
Expand Down

0 comments on commit d645952

Please sign in to comment.