Skip to content

Commit ae37e3c

Browse files
authored
Remove u64 support for Poisson (rust-random#1517)
1 parent 7aa82c8 commit ae37e3c

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff 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)

src/poisson.rs

+11-8
Original file line numberDiff line numberDiff 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))]
4455
pub 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)]
250253
mod test {
251254
use super::*;

src/zeta.rs

+11
Original file line numberDiff line numberDiff 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.

src/zipf.rs

+7
Original file line numberDiff line numberDiff 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),

0 commit comments

Comments
 (0)