Skip to content

Commit

Permalink
distributions: support a zero max value in Zipf.
Browse files Browse the repository at this point in the history
There is no documentation that says zero isn't okay, and the closed interval
[0, k] described by the documentation is perfectly well-defined even when k is
zero. As far as I can tell, there is no reason *not* to support zero: a random
variable that always returns the same value is still a random variable.
absl::Uniform will happily generate on the interval [0, 1) for the same
reason.
PiperOrigin-RevId: 694649518
Change-Id: Ib940406f762a30e27c19c846c45bd908ae8411c3
  • Loading branch information
jacobsa authored and copybara-github committed Nov 8, 2024
1 parent 8924509 commit feb6aab
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
7 changes: 7 additions & 0 deletions absl/random/distributions_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,13 @@ TEST_F(RandomDistributionsTest, Zipf) {
EXPECT_NEAR(6.5944, moments.mean, 2000) << moments;
}

TEST_F(RandomDistributionsTest, ZipfWithZeroMax) {
absl::InsecureBitGen gen;
for (int i = 0; i < 100; ++i) {
EXPECT_EQ(0, absl::Zipf(gen, 0));
}
}

TEST_F(RandomDistributionsTest, Gaussian) {
std::vector<double> values(kSize);

Expand Down
4 changes: 2 additions & 2 deletions absl/random/zipf_distribution.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class zipf_distribution {
public:
using distribution_type = zipf_distribution;

// Preconditions: k > 0, v > 0, q > 1
// Preconditions: k >= 0, v > 0, q > 1
// The precondidtions are validated when NDEBUG is not defined via
// a pair of assert() directives.
// If NDEBUG is defined and either or both of these parameters take invalid
Expand Down Expand Up @@ -152,7 +152,7 @@ zipf_distribution<IntType>::param_type::param_type(
: k_(k), q_(q), v_(v), one_minus_q_(1 - q) {
assert(q > 1);
assert(v > 0);
assert(k > 0);
assert(k >= 0);
one_minus_q_inv_ = 1 / one_minus_q_;

// Setup for the ZRI algorithm (pg 17 of the paper).
Expand Down

0 comments on commit feb6aab

Please sign in to comment.