absl::LogUniform

Produces random integral values whose logarithm is uniformly distributed.

Synopsis

Declared in <absl/random/distributions.h>

template<
    typename IntType,
    typename URBG>
IntType
LogUniform(
    URBG&& urbg,
    IntType lo,
    IntType hi,
    IntType base = 2);

Description

absl::LogUniform produces random values distributed where the log to a given base of all values is uniform in a closed interval [lo, hi]. T must be an integral type, but may be inferred from the types of lo and hi.

I.e., LogUniform(0, n, b) is uniformly distributed across buckets [0], [1, b-1], [b, b^2-1].. [b^(k-1), (b^k)-1].. [b^floor(log(n, b)), n] and is uniformly distributed within each bucket.

The resulting probability density is inversely related to bucket size, though values in the final bucket may be more likely than previous values. (In the extreme case where n = b^i the final value will be tied with zero as the most probable result.

If lo is nonzero then this distribution is shifted to the desired interval, so LogUniform(lo, hi, b) is equivalent to LogUniform(0, hi-lo, b)+lo.

See https://en.wikipedia.org/wiki/Reciprocal_distribution

Example:

absl::BitGen bitgen; ... int v = absl::LogUniform(bitgen, 0, 1000);

Return Value

A random value drawn from the log-uniform distribution.

Parameters

NameDescription
urbgThe uniform random bit generator.
loThe inclusive lower bound of the interval.
hiThe inclusive upper bound of the interval.
baseThe logarithm base determining the bucket sizes.