gaussian_distribution_test.cc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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. #include "absl/random/gaussian_distribution.h"
  15. #include <algorithm>
  16. #include <cmath>
  17. #include <cstddef>
  18. #include <ios>
  19. #include <iterator>
  20. #include <random>
  21. #include <string>
  22. #include <type_traits>
  23. #include <vector>
  24. #include "gmock/gmock.h"
  25. #include "gtest/gtest.h"
  26. #include "absl/base/internal/raw_logging.h"
  27. #include "absl/base/macros.h"
  28. #include "absl/numeric/internal/representation.h"
  29. #include "absl/random/internal/chi_square.h"
  30. #include "absl/random/internal/distribution_test_util.h"
  31. #include "absl/random/internal/sequence_urbg.h"
  32. #include "absl/random/random.h"
  33. #include "absl/strings/str_cat.h"
  34. #include "absl/strings/str_format.h"
  35. #include "absl/strings/str_replace.h"
  36. #include "absl/strings/strip.h"
  37. namespace {
  38. using absl::random_internal::kChiSquared;
  39. template <typename RealType>
  40. class GaussianDistributionInterfaceTest : public ::testing::Test {};
  41. // double-double arithmetic is not supported well by either GCC or Clang; see
  42. // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99048,
  43. // https://bugs.llvm.org/show_bug.cgi?id=49131, and
  44. // https://bugs.llvm.org/show_bug.cgi?id=49132. Don't bother running these tests
  45. // with double doubles until compiler support is better.
  46. using RealTypes =
  47. std::conditional<absl::numeric_internal::IsDoubleDouble(),
  48. ::testing::Types<float, double>,
  49. ::testing::Types<float, double, long double>>::type;
  50. TYPED_TEST_CASE(GaussianDistributionInterfaceTest, RealTypes);
  51. TYPED_TEST(GaussianDistributionInterfaceTest, SerializeTest) {
  52. using param_type =
  53. typename absl::gaussian_distribution<TypeParam>::param_type;
  54. const TypeParam kParams[] = {
  55. // Cases around 1.
  56. 1, //
  57. std::nextafter(TypeParam(1), TypeParam(0)), // 1 - epsilon
  58. std::nextafter(TypeParam(1), TypeParam(2)), // 1 + epsilon
  59. // Arbitrary values.
  60. TypeParam(1e-8), TypeParam(1e-4), TypeParam(2), TypeParam(1e4),
  61. TypeParam(1e8), TypeParam(1e20), TypeParam(2.5),
  62. // Boundary cases.
  63. std::numeric_limits<TypeParam>::infinity(),
  64. std::numeric_limits<TypeParam>::max(),
  65. std::numeric_limits<TypeParam>::epsilon(),
  66. std::nextafter(std::numeric_limits<TypeParam>::min(),
  67. TypeParam(1)), // min + epsilon
  68. std::numeric_limits<TypeParam>::min(), // smallest normal
  69. // There are some errors dealing with denorms on apple platforms.
  70. std::numeric_limits<TypeParam>::denorm_min(), // smallest denorm
  71. std::numeric_limits<TypeParam>::min() / 2,
  72. std::nextafter(std::numeric_limits<TypeParam>::min(),
  73. TypeParam(0)), // denorm_max
  74. };
  75. constexpr int kCount = 1000;
  76. absl::InsecureBitGen gen;
  77. // Use a loop to generate the combinations of {+/-x, +/-y}, and assign x, y to
  78. // all values in kParams,
  79. for (const auto mod : {0, 1, 2, 3}) {
  80. for (const auto x : kParams) {
  81. if (!std::isfinite(x)) continue;
  82. for (const auto y : kParams) {
  83. const TypeParam mean = (mod & 0x1) ? -x : x;
  84. const TypeParam stddev = (mod & 0x2) ? -y : y;
  85. const param_type param(mean, stddev);
  86. absl::gaussian_distribution<TypeParam> before(mean, stddev);
  87. EXPECT_EQ(before.mean(), param.mean());
  88. EXPECT_EQ(before.stddev(), param.stddev());
  89. {
  90. absl::gaussian_distribution<TypeParam> via_param(param);
  91. EXPECT_EQ(via_param, before);
  92. EXPECT_EQ(via_param.param(), before.param());
  93. }
  94. // Smoke test.
  95. auto sample_min = before.max();
  96. auto sample_max = before.min();
  97. for (int i = 0; i < kCount; i++) {
  98. auto sample = before(gen);
  99. if (sample > sample_max) sample_max = sample;
  100. if (sample < sample_min) sample_min = sample;
  101. EXPECT_GE(sample, before.min()) << before;
  102. EXPECT_LE(sample, before.max()) << before;
  103. }
  104. if (!std::is_same<TypeParam, long double>::value) {
  105. ABSL_INTERNAL_LOG(
  106. INFO, absl::StrFormat("Range{%f, %f}: %f, %f", mean, stddev,
  107. sample_min, sample_max));
  108. }
  109. std::stringstream ss;
  110. ss << before;
  111. if (!std::isfinite(mean) || !std::isfinite(stddev)) {
  112. // Streams do not parse inf/nan.
  113. continue;
  114. }
  115. // Validate stream serialization.
  116. absl::gaussian_distribution<TypeParam> after(-0.53f, 2.3456f);
  117. EXPECT_NE(before.mean(), after.mean());
  118. EXPECT_NE(before.stddev(), after.stddev());
  119. EXPECT_NE(before.param(), after.param());
  120. EXPECT_NE(before, after);
  121. ss >> after;
  122. EXPECT_EQ(before.mean(), after.mean());
  123. EXPECT_EQ(before.stddev(), after.stddev()) //
  124. << ss.str() << " " //
  125. << (ss.good() ? "good " : "") //
  126. << (ss.bad() ? "bad " : "") //
  127. << (ss.eof() ? "eof " : "") //
  128. << (ss.fail() ? "fail " : "");
  129. }
  130. }
  131. }
  132. }
  133. // http://www.itl.nist.gov/div898/handbook/eda/section3/eda3661.htm
  134. class GaussianModel {
  135. public:
  136. GaussianModel(double mean, double stddev) : mean_(mean), stddev_(stddev) {}
  137. double mean() const { return mean_; }
  138. double variance() const { return stddev() * stddev(); }
  139. double stddev() const { return stddev_; }
  140. double skew() const { return 0; }
  141. double kurtosis() const { return 3.0; }
  142. // The inverse CDF, or PercentPoint function.
  143. double InverseCDF(double p) {
  144. ABSL_ASSERT(p >= 0.0);
  145. ABSL_ASSERT(p < 1.0);
  146. return mean() + stddev() * -absl::random_internal::InverseNormalSurvival(p);
  147. }
  148. private:
  149. const double mean_;
  150. const double stddev_;
  151. };
  152. struct Param {
  153. double mean;
  154. double stddev;
  155. double p_fail; // Z-Test probability of failure.
  156. int trials; // Z-Test trials.
  157. };
  158. // GaussianDistributionTests implements a z-test for the gaussian
  159. // distribution.
  160. class GaussianDistributionTests : public testing::TestWithParam<Param>,
  161. public GaussianModel {
  162. public:
  163. GaussianDistributionTests()
  164. : GaussianModel(GetParam().mean, GetParam().stddev) {}
  165. // SingleZTest provides a basic z-squared test of the mean vs. expected
  166. // mean for data generated by the poisson distribution.
  167. template <typename D>
  168. bool SingleZTest(const double p, const size_t samples);
  169. // SingleChiSquaredTest provides a basic chi-squared test of the normal
  170. // distribution.
  171. template <typename D>
  172. double SingleChiSquaredTest();
  173. // We use a fixed bit generator for distribution accuracy tests. This allows
  174. // these tests to be deterministic, while still testing the qualify of the
  175. // implementation.
  176. absl::random_internal::pcg64_2018_engine rng_{0x2B7E151628AED2A6};
  177. };
  178. template <typename D>
  179. bool GaussianDistributionTests::SingleZTest(const double p,
  180. const size_t samples) {
  181. D dis(mean(), stddev());
  182. std::vector<double> data;
  183. data.reserve(samples);
  184. for (size_t i = 0; i < samples; i++) {
  185. const double x = dis(rng_);
  186. data.push_back(x);
  187. }
  188. const double max_err = absl::random_internal::MaxErrorTolerance(p);
  189. const auto m = absl::random_internal::ComputeDistributionMoments(data);
  190. const double z = absl::random_internal::ZScore(mean(), m);
  191. const bool pass = absl::random_internal::Near("z", z, 0.0, max_err);
  192. // NOTE: Informational statistical test:
  193. //
  194. // Compute the Jarque-Bera test statistic given the excess skewness
  195. // and kurtosis. The statistic is drawn from a chi-square(2) distribution.
  196. // https://en.wikipedia.org/wiki/Jarque%E2%80%93Bera_test
  197. //
  198. // The null-hypothesis (normal distribution) is rejected when
  199. // (p = 0.05 => jb > 5.99)
  200. // (p = 0.01 => jb > 9.21)
  201. // NOTE: JB has a large type-I error rate, so it will reject the
  202. // null-hypothesis even when it is true more often than the z-test.
  203. //
  204. const double jb =
  205. static_cast<double>(m.n) / 6.0 *
  206. (std::pow(m.skewness, 2.0) + std::pow(m.kurtosis - 3.0, 2.0) / 4.0);
  207. if (!pass || jb > 9.21) {
  208. ABSL_INTERNAL_LOG(
  209. INFO, absl::StrFormat("p=%f max_err=%f\n"
  210. " mean=%f vs. %f\n"
  211. " stddev=%f vs. %f\n"
  212. " skewness=%f vs. %f\n"
  213. " kurtosis=%f vs. %f\n"
  214. " z=%f vs. 0\n"
  215. " jb=%f vs. 9.21",
  216. p, max_err, m.mean, mean(), std::sqrt(m.variance),
  217. stddev(), m.skewness, skew(), m.kurtosis,
  218. kurtosis(), z, jb));
  219. }
  220. return pass;
  221. }
  222. template <typename D>
  223. double GaussianDistributionTests::SingleChiSquaredTest() {
  224. const size_t kSamples = 10000;
  225. const int kBuckets = 50;
  226. // The InverseCDF is the percent point function of the
  227. // distribution, and can be used to assign buckets
  228. // roughly uniformly.
  229. std::vector<double> cutoffs;
  230. const double kInc = 1.0 / static_cast<double>(kBuckets);
  231. for (double p = kInc; p < 1.0; p += kInc) {
  232. cutoffs.push_back(InverseCDF(p));
  233. }
  234. if (cutoffs.back() != std::numeric_limits<double>::infinity()) {
  235. cutoffs.push_back(std::numeric_limits<double>::infinity());
  236. }
  237. D dis(mean(), stddev());
  238. std::vector<int32_t> counts(cutoffs.size(), 0);
  239. for (int j = 0; j < kSamples; j++) {
  240. const double x = dis(rng_);
  241. auto it = std::upper_bound(cutoffs.begin(), cutoffs.end(), x);
  242. counts[std::distance(cutoffs.begin(), it)]++;
  243. }
  244. // Null-hypothesis is that the distribution is a gaussian distribution
  245. // with the provided mean and stddev (not estimated from the data).
  246. const int dof = static_cast<int>(counts.size()) - 1;
  247. // Our threshold for logging is 1-in-50.
  248. const double threshold = absl::random_internal::ChiSquareValue(dof, 0.98);
  249. const double expected =
  250. static_cast<double>(kSamples) / static_cast<double>(counts.size());
  251. double chi_square = absl::random_internal::ChiSquareWithExpected(
  252. std::begin(counts), std::end(counts), expected);
  253. double p = absl::random_internal::ChiSquarePValue(chi_square, dof);
  254. // Log if the chi_square value is above the threshold.
  255. if (chi_square > threshold) {
  256. for (int i = 0; i < cutoffs.size(); i++) {
  257. ABSL_INTERNAL_LOG(
  258. INFO, absl::StrFormat("%d : (%f) = %d", i, cutoffs[i], counts[i]));
  259. }
  260. ABSL_INTERNAL_LOG(
  261. INFO, absl::StrCat("mean=", mean(), " stddev=", stddev(), "\n", //
  262. " expected ", expected, "\n", //
  263. kChiSquared, " ", chi_square, " (", p, ")\n", //
  264. kChiSquared, " @ 0.98 = ", threshold));
  265. }
  266. return p;
  267. }
  268. TEST_P(GaussianDistributionTests, ZTest) {
  269. // TODO(absl-team): Run these tests against std::normal_distribution<double>
  270. // to validate outcomes are similar.
  271. const size_t kSamples = 10000;
  272. const auto& param = GetParam();
  273. const int expected_failures =
  274. std::max(1, static_cast<int>(std::ceil(param.trials * param.p_fail)));
  275. const double p = absl::random_internal::RequiredSuccessProbability(
  276. param.p_fail, param.trials);
  277. int failures = 0;
  278. for (int i = 0; i < param.trials; i++) {
  279. failures +=
  280. SingleZTest<absl::gaussian_distribution<double>>(p, kSamples) ? 0 : 1;
  281. }
  282. EXPECT_LE(failures, expected_failures);
  283. }
  284. TEST_P(GaussianDistributionTests, ChiSquaredTest) {
  285. const int kTrials = 20;
  286. int failures = 0;
  287. for (int i = 0; i < kTrials; i++) {
  288. double p_value =
  289. SingleChiSquaredTest<absl::gaussian_distribution<double>>();
  290. if (p_value < 0.0025) { // 1/400
  291. failures++;
  292. }
  293. }
  294. // There is a 0.05% chance of producing at least one failure, so raise the
  295. // failure threshold high enough to allow for a flake rate of less than one in
  296. // 10,000.
  297. EXPECT_LE(failures, 4);
  298. }
  299. std::vector<Param> GenParams() {
  300. return {
  301. // Mean around 0.
  302. Param{0.0, 1.0, 0.01, 100},
  303. Param{0.0, 1e2, 0.01, 100},
  304. Param{0.0, 1e4, 0.01, 100},
  305. Param{0.0, 1e8, 0.01, 100},
  306. Param{0.0, 1e16, 0.01, 100},
  307. Param{0.0, 1e-3, 0.01, 100},
  308. Param{0.0, 1e-5, 0.01, 100},
  309. Param{0.0, 1e-9, 0.01, 100},
  310. Param{0.0, 1e-17, 0.01, 100},
  311. // Mean around 1.
  312. Param{1.0, 1.0, 0.01, 100},
  313. Param{1.0, 1e2, 0.01, 100},
  314. Param{1.0, 1e-2, 0.01, 100},
  315. // Mean around 100 / -100
  316. Param{1e2, 1.0, 0.01, 100},
  317. Param{-1e2, 1.0, 0.01, 100},
  318. Param{1e2, 1e6, 0.01, 100},
  319. Param{-1e2, 1e6, 0.01, 100},
  320. // More extreme
  321. Param{1e4, 1e4, 0.01, 100},
  322. Param{1e8, 1e4, 0.01, 100},
  323. Param{1e12, 1e4, 0.01, 100},
  324. };
  325. }
  326. std::string ParamName(const ::testing::TestParamInfo<Param>& info) {
  327. const auto& p = info.param;
  328. std::string name = absl::StrCat("mean_", absl::SixDigits(p.mean), "__stddev_",
  329. absl::SixDigits(p.stddev));
  330. return absl::StrReplaceAll(name, {{"+", "_"}, {"-", "_"}, {".", "_"}});
  331. }
  332. INSTANTIATE_TEST_SUITE_P(All, GaussianDistributionTests,
  333. ::testing::ValuesIn(GenParams()), ParamName);
  334. // NOTE: absl::gaussian_distribution is not guaranteed to be stable.
  335. TEST(GaussianDistributionTest, StabilityTest) {
  336. // absl::gaussian_distribution stability relies on the underlying zignor
  337. // data, absl::random_interna::RandU64ToDouble, std::exp, std::log, and
  338. // std::abs.
  339. absl::random_internal::sequence_urbg urbg(
  340. {0x0003eb76f6f7f755ull, 0xFFCEA50FDB2F953Bull, 0xC332DDEFBE6C5AA5ull,
  341. 0x6558218568AB9702ull, 0x2AEF7DAD5B6E2F84ull, 0x1521B62829076170ull,
  342. 0xECDD4775619F1510ull, 0x13CCA830EB61BD96ull, 0x0334FE1EAA0363CFull,
  343. 0xB5735C904C70A239ull, 0xD59E9E0BCBAADE14ull, 0xEECC86BC60622CA7ull});
  344. std::vector<int> output(11);
  345. {
  346. absl::gaussian_distribution<double> dist;
  347. std::generate(std::begin(output), std::end(output),
  348. [&] { return static_cast<int>(10000000.0 * dist(urbg)); });
  349. EXPECT_EQ(13, urbg.invocations());
  350. EXPECT_THAT(output, //
  351. testing::ElementsAre(1494, 25518841, 9991550, 1351856,
  352. -20373238, 3456682, 333530, -6804981,
  353. -15279580, -16459654, 1494));
  354. }
  355. urbg.reset();
  356. {
  357. absl::gaussian_distribution<float> dist;
  358. std::generate(std::begin(output), std::end(output),
  359. [&] { return static_cast<int>(1000000.0f * dist(urbg)); });
  360. EXPECT_EQ(13, urbg.invocations());
  361. EXPECT_THAT(
  362. output, //
  363. testing::ElementsAre(149, 2551884, 999155, 135185, -2037323, 345668,
  364. 33353, -680498, -1527958, -1645965, 149));
  365. }
  366. }
  367. // This is an implementation-specific test. If any part of the implementation
  368. // changes, then it is likely that this test will change as well.
  369. // Also, if dependencies of the distribution change, such as RandU64ToDouble,
  370. // then this is also likely to change.
  371. TEST(GaussianDistributionTest, AlgorithmBounds) {
  372. absl::gaussian_distribution<double> dist;
  373. // In ~95% of cases, a single value is used to generate the output.
  374. // for all inputs where |x| < 0.750461021389 this should be the case.
  375. //
  376. // The exact constraints are based on the ziggurat tables, and any
  377. // changes to the ziggurat tables may require adjusting these bounds.
  378. //
  379. // for i in range(0, len(X)-1):
  380. // print i, X[i+1]/X[i], (X[i+1]/X[i] > 0.984375)
  381. //
  382. // 0.125 <= |values| <= 0.75
  383. const uint64_t kValues[] = {
  384. 0x1000000000000100ull, 0x2000000000000100ull, 0x3000000000000100ull,
  385. 0x4000000000000100ull, 0x5000000000000100ull, 0x6000000000000100ull,
  386. // negative values
  387. 0x9000000000000100ull, 0xa000000000000100ull, 0xb000000000000100ull,
  388. 0xc000000000000100ull, 0xd000000000000100ull, 0xe000000000000100ull};
  389. // 0.875 <= |values| <= 0.984375
  390. const uint64_t kExtraValues[] = {
  391. 0x7000000000000100ull, 0x7800000000000100ull, //
  392. 0x7c00000000000100ull, 0x7e00000000000100ull, //
  393. // negative values
  394. 0xf000000000000100ull, 0xf800000000000100ull, //
  395. 0xfc00000000000100ull, 0xfe00000000000100ull};
  396. auto make_box = [](uint64_t v, uint64_t box) {
  397. return (v & 0xffffffffffffff80ull) | box;
  398. };
  399. // The box is the lower 7 bits of the value. When the box == 0, then
  400. // the algorithm uses an escape hatch to select the result for large
  401. // outputs.
  402. for (uint64_t box = 0; box < 0x7f; box++) {
  403. for (const uint64_t v : kValues) {
  404. // Extra values are added to the sequence to attempt to avoid
  405. // infinite loops from rejection sampling on bugs/errors.
  406. absl::random_internal::sequence_urbg urbg(
  407. {make_box(v, box), 0x0003eb76f6f7f755ull, 0x5FCEA50FDB2F953Bull});
  408. auto a = dist(urbg);
  409. EXPECT_EQ(1, urbg.invocations()) << box << " " << std::hex << v;
  410. if (v & 0x8000000000000000ull) {
  411. EXPECT_LT(a, 0.0) << box << " " << std::hex << v;
  412. } else {
  413. EXPECT_GT(a, 0.0) << box << " " << std::hex << v;
  414. }
  415. }
  416. if (box > 10 && box < 100) {
  417. // The center boxes use the fast algorithm for more
  418. // than 98.4375% of values.
  419. for (const uint64_t v : kExtraValues) {
  420. absl::random_internal::sequence_urbg urbg(
  421. {make_box(v, box), 0x0003eb76f6f7f755ull, 0x5FCEA50FDB2F953Bull});
  422. auto a = dist(urbg);
  423. EXPECT_EQ(1, urbg.invocations()) << box << " " << std::hex << v;
  424. if (v & 0x8000000000000000ull) {
  425. EXPECT_LT(a, 0.0) << box << " " << std::hex << v;
  426. } else {
  427. EXPECT_GT(a, 0.0) << box << " " << std::hex << v;
  428. }
  429. }
  430. }
  431. }
  432. // When the box == 0, the fallback algorithm uses a ratio of uniforms,
  433. // which consumes 2 additional values from the urbg.
  434. // Fallback also requires that the initial value be > 0.9271586026096681.
  435. auto make_fallback = [](uint64_t v) { return (v & 0xffffffffffffff80ull); };
  436. double tail[2];
  437. {
  438. // 0.9375
  439. absl::random_internal::sequence_urbg urbg(
  440. {make_fallback(0x7800000000000000ull), 0x13CCA830EB61BD96ull,
  441. 0x00000076f6f7f755ull});
  442. tail[0] = dist(urbg);
  443. EXPECT_EQ(3, urbg.invocations());
  444. EXPECT_GT(tail[0], 0);
  445. }
  446. {
  447. // -0.9375
  448. absl::random_internal::sequence_urbg urbg(
  449. {make_fallback(0xf800000000000000ull), 0x13CCA830EB61BD96ull,
  450. 0x00000076f6f7f755ull});
  451. tail[1] = dist(urbg);
  452. EXPECT_EQ(3, urbg.invocations());
  453. EXPECT_LT(tail[1], 0);
  454. }
  455. EXPECT_EQ(tail[0], -tail[1]);
  456. EXPECT_EQ(418610, static_cast<int64_t>(tail[0] * 100000.0));
  457. // When the box != 0, the fallback algorithm computes a wedge function.
  458. // Depending on the box, the threshold for varies as high as
  459. // 0.991522480228.
  460. {
  461. // 0.9921875, 0.875
  462. absl::random_internal::sequence_urbg urbg(
  463. {make_box(0x7f00000000000000ull, 120), 0xe000000000000001ull,
  464. 0x13CCA830EB61BD96ull});
  465. tail[0] = dist(urbg);
  466. EXPECT_EQ(2, urbg.invocations());
  467. EXPECT_GT(tail[0], 0);
  468. }
  469. {
  470. // -0.9921875, 0.875
  471. absl::random_internal::sequence_urbg urbg(
  472. {make_box(0xff00000000000000ull, 120), 0xe000000000000001ull,
  473. 0x13CCA830EB61BD96ull});
  474. tail[1] = dist(urbg);
  475. EXPECT_EQ(2, urbg.invocations());
  476. EXPECT_LT(tail[1], 0);
  477. }
  478. EXPECT_EQ(tail[0], -tail[1]);
  479. EXPECT_EQ(61948, static_cast<int64_t>(tail[0] * 100000.0));
  480. // Fallback rejected, try again.
  481. {
  482. // -0.9921875, 0.0625
  483. absl::random_internal::sequence_urbg urbg(
  484. {make_box(0xff00000000000000ull, 120), 0x1000000000000001,
  485. make_box(0x1000000000000100ull, 50), 0x13CCA830EB61BD96ull});
  486. dist(urbg);
  487. EXPECT_EQ(3, urbg.invocations());
  488. }
  489. }
  490. } // namespace