File tree Expand file tree Collapse file tree 5 files changed +33
-12
lines changed Expand file tree Collapse file tree 5 files changed +33
-12
lines changed Original file line number Diff line number Diff line change @@ -21,14 +21,11 @@ You may also find the [Upgrade Guide](https://rust-random.github.io/book/update.
2121- Rename ` rand::distributions ` to ` rand::distr ` (#1470 )
2222- The ` serde1 ` feature has been renamed ` serde ` (#1477 )
2323- The implicit feature ` rand_chacha ` has been removed. This is enabled by ` std_rng ` . (#1473 )
24- - Mark ` WeightError ` , ` PoissonError ` , ` BinomialError ` as ` #[non_exhaustive] ` (#1480 ).
24+ - Mark ` WeightError ` as ` #[non_exhaustive] ` (#1480 ).
2525- Add ` p() ` for ` Bernoulli ` to access probability (#1481 )
2626- Add ` UniformUsize ` and use to make ` Uniform ` for ` usize ` portable (#1487 )
27- - Remove support for generating ` isize ` and ` usize ` values with ` Standard ` , ` Uniform ` and ` Fill ` and usage as a ` WeightedAliasIndex ` weight (#1487 )
2827- Require ` Clone ` and ` AsRef ` bound for ` SeedableRng::Seed ` . (#1491 )
2928- Improve SmallRng initialization performance (#1482 )
30- - Implement ` Distribution<u64> ` for ` Poisson<f64> ` (#1498 )
31- - Limit the maximal acceptable lambda for ` Poisson ` to solve (#1312 ) (#1498 )
3229- Rename ` Rng::gen_iter ` to ` random_iter ` (#1500 )
3330- Rename ` rand::thread_rng() ` to ` rand::rng() ` , and remove from the prelude (#1506 )
3431- Remove ` rand::random() ` from the prelude (#1506 )
Original file line number Diff line number Diff line change @@ -10,6 +10,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010- Move some of the computations in Binomial from ` sample ` to ` new ` (#1484 )
1111- Add Kolmogorov Smirnov test for sampling of ` Normal ` and ` Binomial ` (#1494 )
1212- Add Kolmogorov Smirnov test for more distributions (#1504 )
13+ - Mark ` WeightError ` , ` PoissonError ` , ` BinomialError ` as ` #[non_exhaustive] ` (#1480 ).
14+ - Remove support for generating ` isize ` and ` usize ` values with ` Standard ` , ` Uniform ` and ` Fill ` and usage as a ` WeightedAliasIndex ` weight (#1487 )
15+ - Limit the maximal acceptable lambda for ` Poisson ` to solve (#1312 ) (#1498 )
1316
1417### Added
1518- Add plots for ` rand_distr ` distributions to documentation (#1434 )
Original file line number Diff line number Diff line change @@ -39,6 +39,17 @@ use rand::Rng;
3939/// let v: f64 = poi.sample(&mut rand::rng());
4040/// println!("{} is from a Poisson(2) distribution", v);
4141/// ```
42+ ///
43+ /// # Integer vs FP return type
44+ ///
45+ /// This implementation uses floating-point (FP) logic internally.
46+ ///
47+ /// Due to the parameter limit <code>λ < [Self::MAX_LAMBDA]</code>, it
48+ /// statistically impossible to sample a value larger [`u64::MAX`]. As such, it
49+ /// is reasonable to cast generated samples to `u64` using `as`:
50+ /// `distr.sample(&mut rng) as u64` (and memory safe since Rust 1.45).
51+ /// Similarly, when `λ < 4.2e9` it can be safely assumed that samples are less
52+ /// than `u32::MAX`.
4253#[ derive( Clone , Copy , Debug , PartialEq ) ]
4354#[ cfg_attr( feature = "serde" , derive( serde:: Serialize , serde:: Deserialize ) ) ]
4455pub struct Poisson < F > ( Method < F > )
@@ -238,14 +249,6 @@ where
238249 }
239250}
240251
241- impl Distribution < u64 > for Poisson < f64 > {
242- #[ inline]
243- fn sample < R : Rng + ?Sized > ( & self , rng : & mut R ) -> u64 {
244- // `as` from float to int saturates
245- <Poisson < f64 > as Distribution < f64 > >:: sample ( self , rng) as u64
246- }
247- }
248-
249252#[ cfg( test) ]
250253mod test {
251254 use super :: * ;
Original file line number Diff line number Diff line change @@ -40,6 +40,17 @@ use rand::{distr::OpenClosed01, Rng};
4040/// println!("{}", val);
4141/// ```
4242///
43+ /// # Integer vs FP return type
44+ ///
45+ /// This implementation uses floating-point (FP) logic internally, which can
46+ /// potentially generate very large samples (exceeding e.g. `u64::MAX`).
47+ ///
48+ /// It is *safe* to cast such results to an integer type using `as`
49+ /// (e.g. `distr.sample(&mut rng) as u64`), since such casts are saturating
50+ /// (e.g. `2f64.powi(64) as u64 == u64::MAX`). It is up to the user to
51+ /// determine whether this potential loss of accuracy is acceptable
52+ /// (this determination may depend on the distribution's parameters).
53+ ///
4354/// # Notes
4455///
4556/// The zeta distribution has no upper limit. Sampled values may be infinite.
Original file line number Diff line number Diff line change @@ -39,6 +39,13 @@ use rand::Rng;
3939/// println!("{}", val);
4040/// ```
4141///
42+ /// # Integer vs FP return type
43+ ///
44+ /// This implementation uses floating-point (FP) logic internally. It may be
45+ /// expected that the samples are no greater than `n`, thus it is reasonable to
46+ /// cast generated samples to any integer type which can also represent `n`
47+ /// (e.g. `distr.sample(&mut rng) as u64`).
48+ ///
4249/// # Implementation details
4350///
4451/// Implemented via [rejection sampling](https://en.wikipedia.org/wiki/Rejection_sampling),
You can’t perform that action at this time.
0 commit comments