File tree 4 files changed +32
-8
lines changed
4 files changed +32
-8
lines changed 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
10
10
- Move some of the computations in Binomial from ` sample ` to ` new ` (#1484 )
11
11
- Add Kolmogorov Smirnov test for sampling of ` Normal ` and ` Binomial ` (#1494 )
12
12
- 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 )
13
16
14
17
### Added
15
18
- 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;
39
39
/// let v: f64 = poi.sample(&mut rand::rng());
40
40
/// println!("{} is from a Poisson(2) distribution", v);
41
41
/// ```
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`.
42
53
#[ derive( Clone , Copy , Debug , PartialEq ) ]
43
54
#[ cfg_attr( feature = "serde" , derive( serde:: Serialize , serde:: Deserialize ) ) ]
44
55
pub struct Poisson < F > ( Method < F > )
@@ -238,14 +249,6 @@ where
238
249
}
239
250
}
240
251
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
-
249
252
#[ cfg( test) ]
250
253
mod test {
251
254
use super :: * ;
Original file line number Diff line number Diff line change @@ -40,6 +40,17 @@ use rand::{distr::OpenClosed01, Rng};
40
40
/// println!("{}", val);
41
41
/// ```
42
42
///
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
+ ///
43
54
/// # Notes
44
55
///
45
56
/// 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;
39
39
/// println!("{}", val);
40
40
/// ```
41
41
///
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
+ ///
42
49
/// # Implementation details
43
50
///
44
51
/// Implemented via [rejection sampling](https://en.wikipedia.org/wiki/Rejection_sampling),
You can’t perform that action at this time.
0 commit comments