File tree 5 files changed +33
-12
lines changed
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.
21
21
- Rename ` rand::distributions ` to ` rand::distr ` (#1470 )
22
22
- The ` serde1 ` feature has been renamed ` serde ` (#1477 )
23
23
- 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 ).
25
25
- Add ` p() ` for ` Bernoulli ` to access probability (#1481 )
26
26
- 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 )
28
27
- Require ` Clone ` and ` AsRef ` bound for ` SeedableRng::Seed ` . (#1491 )
29
28
- 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 )
32
29
- Rename ` Rng::gen_iter ` to ` random_iter ` (#1500 )
33
30
- Rename ` rand::thread_rng() ` to ` rand::rng() ` , and remove from the prelude (#1506 )
34
31
- 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
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