diff --git a/rand_distr/CHANGELOG.md b/rand_distr/CHANGELOG.md index 13dac1c8a4d..50cd913b21f 100644 --- a/rand_distr/CHANGELOG.md +++ b/rand_distr/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Remove unused fields from `Gamma`, `NormalInverseGaussian` and `Zipf` distributions (#1184) This breaks serialization compatibility with older versions. - Upgrade Rand +- Fix Knuth's method so `Poisson` doesn't return -1.0 for small lambda ## [0.4.3] - 2021-12-30 - Fix `no_std` build (#1208) diff --git a/rand_distr/src/poisson.rs b/rand_distr/src/poisson.rs index 8b9bffd020e..b0b3d15a8ae 100644 --- a/rand_distr/src/poisson.rs +++ b/rand_distr/src/poisson.rs @@ -89,8 +89,8 @@ where F: Float + FloatConst, Standard: Distribution // for low expected values use the Knuth method if self.lambda < F::from(12.0).unwrap() { - let mut result = F::zero(); - let mut p = F::one(); + let mut result = F::one(); + let mut p = rng.gen::(); while p > self.exp_lambda { p = p*rng.gen::(); result = result + F::one(); @@ -161,10 +161,15 @@ mod test { #[test] fn test_poisson_avg() { - test_poisson_avg_gen::(10.0, 0.5); - test_poisson_avg_gen::(15.0, 0.5); - test_poisson_avg_gen::(10.0, 0.5); - test_poisson_avg_gen::(15.0, 0.5); + test_poisson_avg_gen::(10.0, 0.1); + test_poisson_avg_gen::(15.0, 0.1); + + test_poisson_avg_gen::(10.0, 0.1); + test_poisson_avg_gen::(15.0, 0.1); + + //Small lambda will use Knuth's method with exp_lambda == 1.0 + test_poisson_avg_gen::(0.00000000000000005, 0.1); + test_poisson_avg_gen::(0.00000000000000005, 0.1); } #[test]