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.
Trait Implementations§
Source§impl<F> Distribution<F> for Zipf<F>
impl<F> Distribution<F> for Zipf<F>
impl<F> Copy for Zipf<F>
impl<F> StructuralPartialEq for Zipf<F>
Auto Trait Implementations§
impl<F> Freeze for Zipf<F>where
F: Freeze,
impl<F> RefUnwindSafe for Zipf<F>where
F: RefUnwindSafe,
impl<F> Send for Zipf<F>where
F: Send,
impl<F> Sync for Zipf<F>where
F: Sync,
impl<F> Unpin for Zipf<F>where
F: Unpin,
impl<F> UnwindSafe for Zipf<F>where
F: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CheckedAs for T
impl<T> CheckedAs for T
Source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
Source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
Source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<D, T> DistributionExt<T> for Dwhere
D: Distribution<T>,
impl<D, T> DistributionExt<T> for Dwhere
D: Distribution<T>,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> OverflowingAs for T
impl<T> OverflowingAs for T
Source§fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
Source§impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
Source§fn overflowing_cast_from(src: Src) -> (Dst, bool)
fn overflowing_cast_from(src: Src) -> (Dst, bool)
Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> SaturatingAs for T
impl<T> SaturatingAs for T
Source§fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
Source§impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
Source§fn saturating_cast_from(src: Src) -> Dst
fn saturating_cast_from(src: Src) -> Dst
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.