1 // Copyright 2017 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "absl/random/uniform_real_distribution.h"
16
17 #include <cmath>
18 #include <cstdint>
19 #include <iterator>
20 #include <random>
21 #include <sstream>
22 #include <string>
23 #include <vector>
24
25 #include "gmock/gmock.h"
26 #include "gtest/gtest.h"
27 #include "absl/base/internal/raw_logging.h"
28 #include "absl/random/internal/chi_square.h"
29 #include "absl/random/internal/distribution_test_util.h"
30 #include "absl/random/internal/pcg_engine.h"
31 #include "absl/random/internal/sequence_urbg.h"
32 #include "absl/random/random.h"
33 #include "absl/strings/str_cat.h"
34
35 // NOTES:
36 // * Some documentation on generating random real values suggests that
37 // it is possible to use std::nextafter(b, DBL_MAX) to generate a value on
38 // the closed range [a, b]. Unfortunately, that technique is not universally
39 // reliable due to floating point quantization.
40 //
41 // * absl::uniform_real_distribution<float> generates between 2^28 and 2^29
42 // distinct floating point values in the range [0, 1).
43 //
44 // * absl::uniform_real_distribution<float> generates at least 2^23 distinct
45 // floating point values in the range [1, 2). This should be the same as
46 // any other range covered by a single exponent in IEEE 754.
47 //
48 // * absl::uniform_real_distribution<double> generates more than 2^52 distinct
49 // values in the range [0, 1), and should generate at least 2^52 distinct
50 // values in the range of [1, 2).
51 //
52
53 namespace {
54
55 template <typename RealType>
56 class UniformRealDistributionTest : public ::testing::Test {};
57
58 #if defined(__EMSCRIPTEN__)
59 using RealTypes = ::testing::Types<float, double>;
60 #else
61 using RealTypes = ::testing::Types<float, double, long double>;
62 #endif // defined(__EMSCRIPTEN__)
63
64 TYPED_TEST_SUITE(UniformRealDistributionTest, RealTypes);
65
TYPED_TEST(UniformRealDistributionTest,ParamSerializeTest)66 TYPED_TEST(UniformRealDistributionTest, ParamSerializeTest) {
67 using param_type =
68 typename absl::uniform_real_distribution<TypeParam>::param_type;
69
70 constexpr const TypeParam a{1152921504606846976};
71
72 constexpr int kCount = 1000;
73 absl::InsecureBitGen gen;
74 for (const auto& param : {
75 param_type(),
76 param_type(TypeParam(2.0), TypeParam(2.0)), // Same
77 param_type(TypeParam(-0.1), TypeParam(0.1)),
78 param_type(TypeParam(0.05), TypeParam(0.12)),
79 param_type(TypeParam(-0.05), TypeParam(0.13)),
80 param_type(TypeParam(-0.05), TypeParam(-0.02)),
81 // double range = 0
82 // 2^60 , 2^60 + 2^6
83 param_type(a, TypeParam(1152921504606847040)),
84 // 2^60 , 2^60 + 2^7
85 param_type(a, TypeParam(1152921504606847104)),
86 // double range = 2^8
87 // 2^60 , 2^60 + 2^8
88 param_type(a, TypeParam(1152921504606847232)),
89 // float range = 0
90 // 2^60 , 2^60 + 2^36
91 param_type(a, TypeParam(1152921573326323712)),
92 // 2^60 , 2^60 + 2^37
93 param_type(a, TypeParam(1152921642045800448)),
94 // float range = 2^38
95 // 2^60 , 2^60 + 2^38
96 param_type(a, TypeParam(1152921779484753920)),
97 // Limits
98 param_type(0, std::numeric_limits<TypeParam>::max()),
99 param_type(std::numeric_limits<TypeParam>::lowest(), 0),
100 param_type(0, std::numeric_limits<TypeParam>::epsilon()),
101 param_type(-std::numeric_limits<TypeParam>::epsilon(),
102 std::numeric_limits<TypeParam>::epsilon()),
103 param_type(std::numeric_limits<TypeParam>::epsilon(),
104 2 * std::numeric_limits<TypeParam>::epsilon()),
105 }) {
106 // Validate parameters.
107 const auto a = param.a();
108 const auto b = param.b();
109 absl::uniform_real_distribution<TypeParam> before(a, b);
110 EXPECT_EQ(before.a(), param.a());
111 EXPECT_EQ(before.b(), param.b());
112
113 {
114 absl::uniform_real_distribution<TypeParam> via_param(param);
115 EXPECT_EQ(via_param, before);
116 }
117
118 std::stringstream ss;
119 ss << before;
120 absl::uniform_real_distribution<TypeParam> after(TypeParam(1.0),
121 TypeParam(3.1));
122
123 EXPECT_NE(before.a(), after.a());
124 EXPECT_NE(before.b(), after.b());
125 EXPECT_NE(before.param(), after.param());
126 EXPECT_NE(before, after);
127
128 ss >> after;
129
130 EXPECT_EQ(before.a(), after.a());
131 EXPECT_EQ(before.b(), after.b());
132 EXPECT_EQ(before.param(), after.param());
133 EXPECT_EQ(before, after);
134
135 // Smoke test.
136 auto sample_min = after.max();
137 auto sample_max = after.min();
138 for (int i = 0; i < kCount; i++) {
139 auto sample = after(gen);
140 // Failure here indicates a bug in uniform_real_distribution::operator(),
141 // or bad parameters--range too large, etc.
142 if (after.min() == after.max()) {
143 EXPECT_EQ(sample, after.min());
144 } else {
145 EXPECT_GE(sample, after.min());
146 EXPECT_LT(sample, after.max());
147 }
148 if (sample > sample_max) {
149 sample_max = sample;
150 }
151 if (sample < sample_min) {
152 sample_min = sample;
153 }
154 }
155
156 if (!std::is_same<TypeParam, long double>::value) {
157 // static_cast<double>(long double) can overflow.
158 std::string msg = absl::StrCat("Range: ", static_cast<double>(sample_min),
159 ", ", static_cast<double>(sample_max));
160 ABSL_RAW_LOG(INFO, "%s", msg.c_str());
161 }
162 }
163 }
164
165 #ifdef _MSC_VER
166 #pragma warning(push)
167 #pragma warning(disable:4756) // Constant arithmetic overflow.
168 #endif
TYPED_TEST(UniformRealDistributionTest,ViolatesPreconditionsDeathTest)169 TYPED_TEST(UniformRealDistributionTest, ViolatesPreconditionsDeathTest) {
170 #if GTEST_HAS_DEATH_TEST
171 // Hi < Lo
172 EXPECT_DEBUG_DEATH(
173 { absl::uniform_real_distribution<TypeParam> dist(10.0, 1.0); }, "");
174
175 // Hi - Lo > numeric_limits<>::max()
176 EXPECT_DEBUG_DEATH(
177 {
178 absl::uniform_real_distribution<TypeParam> dist(
179 std::numeric_limits<TypeParam>::lowest(),
180 std::numeric_limits<TypeParam>::max());
181 },
182 "");
183 #endif // GTEST_HAS_DEATH_TEST
184 #if defined(NDEBUG)
185 // opt-mode, for invalid parameters, will generate a garbage value,
186 // but should not enter an infinite loop.
187 absl::InsecureBitGen gen;
188 {
189 absl::uniform_real_distribution<TypeParam> dist(10.0, 1.0);
190 auto x = dist(gen);
191 EXPECT_FALSE(std::isnan(x)) << x;
192 }
193 {
194 absl::uniform_real_distribution<TypeParam> dist(
195 std::numeric_limits<TypeParam>::lowest(),
196 std::numeric_limits<TypeParam>::max());
197 auto x = dist(gen);
198 // Infinite result.
199 EXPECT_FALSE(std::isfinite(x)) << x;
200 }
201 #endif // NDEBUG
202 }
203 #ifdef _MSC_VER
204 #pragma warning(pop) // warning(disable:4756)
205 #endif
206
TYPED_TEST(UniformRealDistributionTest,TestMoments)207 TYPED_TEST(UniformRealDistributionTest, TestMoments) {
208 constexpr int kSize = 1000000;
209 std::vector<double> values(kSize);
210
211 // We use a fixed bit generator for distribution accuracy tests. This allows
212 // these tests to be deterministic, while still testing the qualify of the
213 // implementation.
214 absl::random_internal::pcg64_2018_engine rng{0x2B7E151628AED2A6};
215
216 absl::uniform_real_distribution<TypeParam> dist;
217 for (int i = 0; i < kSize; i++) {
218 values[i] = dist(rng);
219 }
220
221 const auto moments =
222 absl::random_internal::ComputeDistributionMoments(values);
223 EXPECT_NEAR(0.5, moments.mean, 0.01);
224 EXPECT_NEAR(1 / 12.0, moments.variance, 0.015);
225 EXPECT_NEAR(0.0, moments.skewness, 0.02);
226 EXPECT_NEAR(9 / 5.0, moments.kurtosis, 0.015);
227 }
228
TYPED_TEST(UniformRealDistributionTest,ChiSquaredTest50)229 TYPED_TEST(UniformRealDistributionTest, ChiSquaredTest50) {
230 using absl::random_internal::kChiSquared;
231 using param_type =
232 typename absl::uniform_real_distribution<TypeParam>::param_type;
233
234 constexpr size_t kTrials = 100000;
235 constexpr int kBuckets = 50;
236 constexpr double kExpected =
237 static_cast<double>(kTrials) / static_cast<double>(kBuckets);
238
239 // 1-in-100000 threshold, but remember, there are about 8 tests
240 // in this file. And the test could fail for other reasons.
241 // Empirically validated with --runs_per_test=10000.
242 const int kThreshold =
243 absl::random_internal::ChiSquareValue(kBuckets - 1, 0.999999);
244
245 // We use a fixed bit generator for distribution accuracy tests. This allows
246 // these tests to be deterministic, while still testing the qualify of the
247 // implementation.
248 absl::random_internal::pcg64_2018_engine rng{0x2B7E151628AED2A6};
249
250 for (const auto& param : {param_type(0, 1), param_type(5, 12),
251 param_type(-5, 13), param_type(-5, -2)}) {
252 const double min_val = param.a();
253 const double max_val = param.b();
254 const double factor = kBuckets / (max_val - min_val);
255
256 std::vector<int32_t> counts(kBuckets, 0);
257 absl::uniform_real_distribution<TypeParam> dist(param);
258 for (size_t i = 0; i < kTrials; i++) {
259 auto x = dist(rng);
260 auto bucket = static_cast<size_t>((x - min_val) * factor);
261 counts[bucket]++;
262 }
263
264 double chi_square = absl::random_internal::ChiSquareWithExpected(
265 std::begin(counts), std::end(counts), kExpected);
266 if (chi_square > kThreshold) {
267 double p_value =
268 absl::random_internal::ChiSquarePValue(chi_square, kBuckets);
269
270 // Chi-squared test failed. Output does not appear to be uniform.
271 std::string msg;
272 for (const auto& a : counts) {
273 absl::StrAppend(&msg, a, "\n");
274 }
275 absl::StrAppend(&msg, kChiSquared, " p-value ", p_value, "\n");
276 absl::StrAppend(&msg, "High ", kChiSquared, " value: ", chi_square, " > ",
277 kThreshold);
278 ABSL_RAW_LOG(INFO, "%s", msg.c_str());
279 FAIL() << msg;
280 }
281 }
282 }
283
TYPED_TEST(UniformRealDistributionTest,StabilityTest)284 TYPED_TEST(UniformRealDistributionTest, StabilityTest) {
285 // absl::uniform_real_distribution stability relies only on
286 // random_internal::RandU64ToDouble and random_internal::RandU64ToFloat.
287 absl::random_internal::sequence_urbg urbg(
288 {0x0003eb76f6f7f755ull, 0xFFCEA50FDB2F953Bull, 0xC332DDEFBE6C5AA5ull,
289 0x6558218568AB9702ull, 0x2AEF7DAD5B6E2F84ull, 0x1521B62829076170ull,
290 0xECDD4775619F1510ull, 0x13CCA830EB61BD96ull, 0x0334FE1EAA0363CFull,
291 0xB5735C904C70A239ull, 0xD59E9E0BCBAADE14ull, 0xEECC86BC60622CA7ull});
292
293 std::vector<int> output(12);
294
295 absl::uniform_real_distribution<TypeParam> dist;
296 std::generate(std::begin(output), std::end(output), [&] {
297 return static_cast<int>(TypeParam(1000000) * dist(urbg));
298 });
299
300 EXPECT_THAT(
301 output, //
302 testing::ElementsAre(59, 999246, 762494, 395876, 167716, 82545, 925251,
303 77341, 12527, 708791, 834451, 932808));
304 }
305
TEST(UniformRealDistributionTest,AlgorithmBounds)306 TEST(UniformRealDistributionTest, AlgorithmBounds) {
307 absl::uniform_real_distribution<double> dist;
308
309 {
310 // This returns the smallest value >0 from absl::uniform_real_distribution.
311 absl::random_internal::sequence_urbg urbg({0x0000000000000001ull});
312 double a = dist(urbg);
313 EXPECT_EQ(a, 5.42101086242752217004e-20);
314 }
315
316 {
317 // This returns a value very near 0.5 from absl::uniform_real_distribution.
318 absl::random_internal::sequence_urbg urbg({0x7fffffffffffffefull});
319 double a = dist(urbg);
320 EXPECT_EQ(a, 0.499999999999999944489);
321 }
322 {
323 // This returns a value very near 0.5 from absl::uniform_real_distribution.
324 absl::random_internal::sequence_urbg urbg({0x8000000000000000ull});
325 double a = dist(urbg);
326 EXPECT_EQ(a, 0.5);
327 }
328
329 {
330 // This returns the largest value <1 from absl::uniform_real_distribution.
331 absl::random_internal::sequence_urbg urbg({0xFFFFFFFFFFFFFFEFull});
332 double a = dist(urbg);
333 EXPECT_EQ(a, 0.999999999999999888978);
334 }
335 {
336 // This *ALSO* returns the largest value <1.
337 absl::random_internal::sequence_urbg urbg({0xFFFFFFFFFFFFFFFFull});
338 double a = dist(urbg);
339 EXPECT_EQ(a, 0.999999999999999888978);
340 }
341 }
342
343 } // namespace
344