Skip to content

Commit feb6aab

Browse files
jacobsacopybara-github
authored andcommitted
distributions: support a zero max value in Zipf.
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
1 parent 8924509 commit feb6aab

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

absl/random/distributions_test.cc

+7
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,13 @@ TEST_F(RandomDistributionsTest, Zipf) {
470470
EXPECT_NEAR(6.5944, moments.mean, 2000) << moments;
471471
}
472472

473+
TEST_F(RandomDistributionsTest, ZipfWithZeroMax) {
474+
absl::InsecureBitGen gen;
475+
for (int i = 0; i < 100; ++i) {
476+
EXPECT_EQ(0, absl::Zipf(gen, 0));
477+
}
478+
}
479+
473480
TEST_F(RandomDistributionsTest, Gaussian) {
474481
std::vector<double> values(kSize);
475482

absl/random/zipf_distribution.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class zipf_distribution {
5757
public:
5858
using distribution_type = zipf_distribution;
5959

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

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

0 commit comments

Comments
 (0)