pub struct Zipf<F>{ /* private fields */ }Expand description
The Zipf (Zipfian) distribution Zipf(n, s).
The samples follow Zipf’s law:
The frequency of each sample from a finite set of size n is inversely
proportional to a power of its frequency rank (with exponent s).
For large n, this converges to the Zeta distribution.
For s = 0, this becomes a uniform distribution.
§Plot
The following plot illustrates the Zipf distribution for n = 10 and
various values of s.
§Example
use rand::prelude::*;
use rand_distr::Zipf;
let val: f64 = rand::rng().sample(Zipf::new(10.0, 1.5).unwrap());
println!("{}", val);§Integer vs FP return type
This implementation uses floating-point (FP) logic internally. It may be
expected that the samples are no greater than n, thus it is reasonable to
cast generated samples to any integer type which can also represent n
(e.g. distr.sample(&mut rng) as u64).
§Implementation details
Implemented via rejection sampling, due to Jason Crease1.
Implementations§
Source§impl<F> Zipf<F>
impl<F> Zipf<F>
Sourcepub fn new(n: F, s: F) -> Result<Zipf<F>, Error>
pub fn new(n: F, s: F) -> Result<Zipf<F>, Error>
Construct a new Zipf distribution for a set with n elements and a
frequency rank exponent s.
The parameter n is typically integral, however we use type
F: [Float] in order to permit very large values
and since our implementation requires a floating-point type.