Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
random_source_test.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
46#include <gtest/gtest.h>
47#include <ah-random.H>
48#include <ah_init_guard.H>
49#include <tpl_array.H>
50#include <tpl_dynSetTree.H>
51
52#include <algorithm>
53#include <cmath>
54#include <cstdint>
55#include <limits>
56#include <numeric>
57#include <optional>
58#include <random>
59#include <stdexcept>
60#include <thread>
61#include <type_traits>
62#include <utility>
63
64using namespace Aleph;
65
66namespace
67{
68 using Engine = Random_Source::Engine;
69
70 // The first numbers of `new SplittableRandom(seed).nextLong()` of Java
71 // 21, for each seed
72 struct Reference
73 {
74 std::uint64_t seed;
75 std::uint64_t numbers[5];
76 };
77
78 constexpr Reference references[] = {
79 {0ULL,
80 {0xe220a8397b1dcdafULL, 0x6e789e6aa1b965f4ULL, 0x06c45d188009454fULL,
81 0xf88bb8a8724c81ecULL, 0x1b39896a51a8749bULL}},
82 {1ULL,
83 {0x910a2dec89025cc1ULL, 0xbeeb8da1658eec67ULL, 0xf893a2eefb32555eULL,
84 0x71c18690ee42c90bULL, 0x71bb54d8d101b5b9ULL}},
85 {1234567ULL,
86 {0x599ed017fb08fc85ULL, 0x2c73f08458540fa5ULL, 0x883ebce5a3f27c77ULL,
87 0x3fbef740e9177b3fULL, 0xe3b8346708cb5ecdULL}},
88 {0xffffffffffffffffULL, // the state wraps around in the first number
89 {0xe4d971771b652c20ULL, 0xe99ff867dbf682c9ULL, 0x382ff84cb27281e9ULL,
90 0x6d1db36ccba982d2ULL, 0xb4a0472e578069aeULL}},
91 };
92
93 // The seeds of the tests: 0, which GSL takes as 4357; the ends of the 32
94 // bits, and one above them, of which GSL takes the low 32
95 constexpr std::uint64_t seeds[] = {0ULL, 1ULL, 12345ULL, 0xfffffffeULL, 0xffffffffULL,
96 0x100000005ULL, 0xffffffffffffffffULL};
97
98 // The bounds up to 2^32 - 1: the least ones, powers of two and their
99 // neighbors, those where half of the numbers are rejected, and the last
100 constexpr std::uint64_t bounds[] = {1ULL, 2ULL, 3ULL, 5ULL, 7ULL, 10ULL, 1000ULL,
101 65535ULL, 65536ULL, 65537ULL, 0x7fffffffULL,
102 0x80000000ULL, 0x80000001ULL, 0xc0000000ULL,
103 0xfffffffeULL, 0xffffffffULL};
104
105 // The bounds above 2^32 - 1: the first ones, powers of two and their
106 // neighbors, and the last
107 constexpr std::uint64_t wide_bounds[] = {0x100000000ULL, 0x100000001ULL, 0x1ffffffffULL,
108 0x200000000ULL, 0x10000003039ULL,
109 0x8000000000000000ULL, 0x8000000000000001ULL,
110 0xffffffffffffffffULL};
111
112 // The number of 32 bits of a source on SplitMix64: the high half
113 std::uint32_t high(SplitMix64_Rng & twin)
114 {
115 return static_cast<std::uint32_t>(twin() >> 32);
116 }
117
118 // The integer below n, up to 2^32 - 1, of a source on SplitMix64, as
119 // the method of Lemire is defined: the high word of the product by n of
120 // the first number whose product has a low word that is not below
121 // 2^32 mod n
122 std::uint64_t lemire(SplitMix64_Rng & twin, const std::uint64_t n)
123 {
124 const std::uint64_t rejected = (std::uint64_t{1} << 32) % n;
125 while (true)
126 {
127 const std::uint64_t product = high(twin) * n;
128 if ((product & 0xffffffffULL) >= rejected)
129 return product >> 32;
130 }
131 }
132
133 // The least mask of ones that covers n - 1, a bit at a time
134 std::uint64_t mask_for(const std::uint64_t n)
135 {
136 std::uint64_t mask = 0;
137 while (mask < n - 1)
138 mask = (mask << 1) | 1;
139 return mask;
140 }
141
142 // The integer below n, above 2^32 - 1: the first number of 64 bits that,
143 // under the mask, is below n
144 template <class Next64>
145 std::uint64_t under_a_mask(Next64 next64, const std::uint64_t n)
146 {
147 const std::uint64_t mask = mask_for(n);
148 while (true)
149 if (const std::uint64_t k = next64() & mask; k < n)
150 return k;
151 }
152
153 // A number of 64 bits of the Mersenne Twister: two of its numbers, the
154 // high half first
155 std::uint64_t two_words(Gsl_Compatible_Rng & twin)
156 {
157 const std::uint64_t first = twin();
158 const std::uint64_t second = twin();
159 return (first << 32) | second;
160 }
161
162 // What a method that gives an integer below n does with each number of
163 // Word as its first one: how many numbers gave each integer at once,
164 // how many were rejected, and how many calls took no number at all
165 struct Tally
166 {
167 Array<size_t> kept;
168 size_t rejected = 0;
169 size_t without_a_number = 0;
170 bool in_range = true;
171 };
172
173 template <class Word, class Method>
174 Tally tally(Method method, const Word n)
175 {
176 Tally t{Array<size_t>(static_cast<size_t>(n), size_t{0})};
177 const std::uint32_t words = std::uint32_t{1} << std::numeric_limits<Word>::digits;
178 for (std::uint32_t first = 0; first < words; ++first)
179 {
180 // were the first number rejected, the next ones are 0, 1, 2 ...,
181 // of which both methods keep one of the first two
182 size_t calls = 0;
183 std::uint32_t later = 0;
184 auto next = [&calls, &later, first] () -> Word
185 {
186 return static_cast<Word>(calls++ == 0 ? first : later++);
187 };
188 const Word k = method(next, n);
189 if (k >= n)
190 {
191 t.in_range = false;
192 continue;
193 }
194 if (calls == 0)
195 ++t.without_a_number;
196 else if (calls == 1)
197 ++t.kept(k);
198 else
199 ++t.rejected;
200 }
201 return t;
202 }
203
204 template <class Word, class Wide>
205 void expect_that_bounded_is_exact(const Word n)
206 {
207 const size_t words = size_t{1} << std::numeric_limits<Word>::digits;
208 const Tally t = tally<Word>([] (auto & next, const Word m)
209 {
210 return random_detail::bounded<Word, Wide>(next, m);
211 }, n);
212 ASSERT_TRUE(t.in_range) << "n = " << +n;
213 EXPECT_EQ(t.without_a_number, 0u) << "n = " << +n;
214 // it rejects the 2^w mod n numbers that are left over, and no more
215 EXPECT_EQ(t.rejected, words % n) << "n = " << +n;
216 for (size_t k = 0; k < n; ++k)
217 ASSERT_EQ(t.kept(k), words / n) << "n = " << +n << ", integer " << k;
218 }
219
220 template <class Word>
221 void expect_that_masked_is_exact(const Word n)
222 {
223 const size_t words = size_t{1} << std::numeric_limits<Word>::digits;
224 const Tally t = tally<Word>([] (auto & next, const Word m)
225 {
226 return random_detail::masked<Word>(next, m);
227 }, n);
228 ASSERT_TRUE(t.in_range) << "n = " << +n;
229 if (n == 1)
230 {
231 EXPECT_EQ(t.without_a_number, words); // no bit to draw
232 return;
233 }
234
235 size_t under_the_mask = 1; // the least power of two that is not below n
236 while (under_the_mask < n)
237 under_the_mask *= 2;
238 EXPECT_EQ(t.without_a_number, 0u) << "n = " << +n;
239 EXPECT_EQ(t.rejected, words - n * (words / under_the_mask)) << "n = " << +n;
240 EXPECT_LT(2 * t.rejected, words) << "n = " << +n; // it keeps more than half
241 for (size_t k = 0; k < n; ++k)
242 ASSERT_EQ(t.kept(k), words / under_the_mask) << "n = " << +n << ", integer " << k;
243 }
244
245 // Whether the counts are those of as many draws among values that are
246 // equally likely: their chi-square, which the transformation of Wilson
247 // and Hilferty takes to a normal deviate (its cube root, for any number
248 // of degrees of freedom), within 6 standard deviations of its mean,
249 // which a right generator leaves once in 10^9 times
250 ::testing::AssertionResult are_uniform(const Array<size_t> & counts, const size_t draws)
251 {
252 const double expected = static_cast<double>(draws) / static_cast<double>(counts.size());
253 double chi2 = 0;
254 for (size_t i = 0; i < counts.size(); ++i)
255 {
256 const double d = static_cast<double>(counts(i)) - expected;
257 chi2 += d * d / expected;
258 }
259 const double freedom = static_cast<double>(counts.size() - 1);
260 const double spread = 2 / (9 * freedom);
261 const double deviate = (std::cbrt(chi2 / freedom) - (1 - spread)) / std::sqrt(spread);
262 if (std::abs(deviate) <= 6)
263 return ::testing::AssertionSuccess();
264 return ::testing::AssertionFailure() << "chi-square " << chi2 << " with " << freedom
265 << " degrees of freedom: " << deviate
266 << " standard deviations";
267 }
268
269 // The state of SplitMix64 that gives a number: mix() undone, step by
270 // step, with the inverses modulo 2^64 of its two multipliers
271 std::uint64_t unmix(std::uint64_t z)
272 {
273 z = z ^ (z >> 31) ^ (z >> 62);
274 z *= 0x319642b2d24d8ec3ULL; // the inverse of 0x94d049bb133111eb
275 z = z ^ (z >> 27) ^ (z >> 54);
276 z *= 0x96de1b173f119089ULL; // the inverse of 0xbf58476d1ce4e5b9
277 return z ^ (z >> 30) ^ (z >> 60);
278 }
279
280 // The seed that the next source built without one takes, which is not
281 // taken here: the number that follows, in the SplitMix64 of the process,
282 // the one that this call spends. For tests of a thread alone
283 std::uint64_t seed_of_the_next_source()
284 {
287 }
288
289 // A source with each engine and the seed s
290 Random_Source source_of(const Engine engine, const std::uint64_t s)
291 {
292 return Random_Source(engine, s);
293 }
294
295 constexpr Engine engines[] = {Engine::SplitMix64, Engine::Mersenne_Twister};
296
297 const char * name_of(const Engine engine)
298 {
299 return engine == Engine::SplitMix64 ? "SplitMix64" : "Mersenne Twister";
300 }
301} // namespace
302
303// ---------------------------------------------------------------------------
304// SplitMix64_Rng
305// ---------------------------------------------------------------------------
306
307TEST(SplitMix64Rng, TheNumbersOfItsReference)
308{
309 for (const Reference & reference : references)
310 {
311 SplitMix64_Rng g(reference.seed);
312 for (const std::uint64_t number : reference.numbers)
313 EXPECT_EQ(g(), number) << "seed " << reference.seed;
314 }
315}
316
317TEST(SplitMix64Rng, TheNumberIsTheMixOfTheStateAdvancedByTheIncrement)
318{
319 static_assert(SplitMix64_Rng::increment % 2 == 1,
320 "an odd increment goes through the 2^64 states");
321 for (const Reference & reference : references)
322 {
323 std::uint64_t state = reference.seed;
324 for (const std::uint64_t number : reference.numbers)
325 EXPECT_EQ(SplitMix64_Rng::mix(state += SplitMix64_Rng::increment), number);
326 }
327 EXPECT_EQ(SplitMix64_Rng::mix(0), 0u);
328}
329
330TEST(SplitMix64Rng, IsAConstantExpression)
331{
332 // built, seeded again, drawn from and compared at compile time
333 constexpr auto third = [] (const std::uint64_t seed)
334 {
336 g.seed(seed);
337 (void) g();
338 (void) g();
339 return g();
340 };
341 static_assert(third(0) == 0x06c45d188009454fULL);
342 static_assert(third(1234567) == 0x883ebce5a3f27c77ULL);
344 static_assert(not (SplitMix64_Rng(1) == SplitMix64_Rng(2)));
345 static_assert(SplitMix64_Rng::min() == 0 and SplitMix64_Rng::max() == ~std::uint64_t{0});
346 static_assert(noexcept(std::declval<SplitMix64_Rng &>()()));
347 SUCCEED();
348}
349
350TEST(SplitMix64Rng, TheDefaultSeedIsZero)
351{
352 SplitMix64_Rng g, zero(0);
353 for (int i = 0; i < 100; ++i)
354 ASSERT_EQ(g(), zero());
355}
356
357TEST(SplitMix64Rng, SeedingAgainIsAsBuildingWithTheSeed)
358{
359 SplitMix64_Rng g(31);
360 for (int i = 0; i < 100; ++i)
361 (void) g();
362
363 for (const Reference & reference : references)
364 {
365 g.seed(reference.seed);
366 EXPECT_EQ(g, SplitMix64_Rng(reference.seed));
367 for (const std::uint64_t number : reference.numbers)
368 EXPECT_EQ(g(), number) << "seed " << reference.seed;
369 }
370}
371
372TEST(SplitMix64Rng, ACopyGoesOnWithTheSameNumbers)
373{
374 SplitMix64_Rng g(31);
375 for (int i = 0; i < 100; ++i)
376 (void) g();
377
378 SplitMix64_Rng c = g;
379 EXPECT_EQ(c, g);
380 for (int i = 0; i < 1000; ++i)
381 ASSERT_EQ(c(), g());
382
383 (void) c();
384 EXPECT_FALSE(c == g);
385}
386
387TEST(SplitMix64Rng, FeedsTheStandardLibrary)
388{
389 static_assert(std::uniform_random_bit_generator<SplitMix64_Rng>);
390 static_assert(sizeof(SplitMix64_Rng) == sizeof(std::uint64_t));
391 static_assert(std::is_trivially_copyable_v<SplitMix64_Rng>);
392
393 Array<int> v(100, 0), w(100, 0);
394 std::iota(v.begin(), v.end(), 0);
395 std::iota(w.begin(), w.end(), 0);
396 SplitMix64_Rng a(5), b(5);
397 std::shuffle(v.begin(), v.end(), a);
398 std::shuffle(w.begin(), w.end(), b);
399 EXPECT_TRUE(std::equal(v.begin(), v.end(), w.begin()));
400 EXPECT_FALSE(std::is_sorted(v.begin(), v.end()));
401}
402
403TEST(SplitMix64Rng, ItsBitsAreBalanced)
404{
405 // each of the 64 bits is set in half of the numbers, within 6 standard
406 // deviations: 100000 numbers of a sequence of consecutive seeds too
407 const int n = 100000;
408 for (const bool consecutive_seeds : {false, true})
409 {
410 Array<size_t> set(64, size_t{0});
411 SplitMix64_Rng g(2026);
412 for (int i = 0; i < n; ++i)
413 {
414 const std::uint64_t x = consecutive_seeds ? SplitMix64_Rng(i)() : g();
415 for (int bit = 0; bit < 64; ++bit)
416 set(bit) += (x >> bit) & 1;
417 }
418 for (int bit = 0; bit < 64; ++bit)
419 EXPECT_NEAR(static_cast<double>(set(bit)), n / 2.0, 6 * std::sqrt(n / 4.0))
420 << "bit " << bit << (consecutive_seeds ? ", consecutive seeds" : "");
421 }
422}
423
424// ---------------------------------------------------------------------------
425// The methods that give an integer below a bound
426// ---------------------------------------------------------------------------
427
428TEST(RandomDetail, BoundedIsExactWithEveryBoundOfWordsOf8Bits)
429{
430 for (unsigned n = 1; n < 256; ++n)
431 expect_that_bounded_is_exact<std::uint8_t, std::uint16_t>(static_cast<std::uint8_t>(n));
432}
433
434TEST(RandomDetail, BoundedIsExactWithWordsOf16Bits)
435{
436 // the classes of the bounds: the least ones, powers of two and their
437 // neighbors, those that reject near half of the numbers, and the last
438 for (const unsigned n : {1u, 2u, 3u, 5u, 10u, 255u, 256u, 257u, 1000u, 32767u, 32768u,
439 32769u, 43691u, 49152u, 65534u, 65535u})
440 expect_that_bounded_is_exact<std::uint16_t, std::uint32_t>(static_cast<std::uint16_t>(n));
441}
442
443TEST(RandomDetail, MaskedIsExactWithEveryBoundOfWordsOf8Bits)
444{
445 for (unsigned n = 1; n < 256; ++n)
446 expect_that_masked_is_exact<std::uint8_t>(static_cast<std::uint8_t>(n));
447}
448
449TEST(RandomDetail, MaskedIsExactWithWordsOf16Bits)
450{
451 for (const unsigned n : {1u, 2u, 3u, 5u, 10u, 255u, 256u, 257u, 1000u, 32767u, 32768u,
452 32769u, 43691u, 49152u, 65534u, 65535u})
453 expect_that_masked_is_exact<std::uint16_t>(static_cast<std::uint16_t>(n));
454}
455
456TEST(RandomDetail, TheSeedsOfTheirOwnAreNumbersOfOneSplitMix64)
457{
458 // each one is the next number of a SplitMix64 of the process: the
459 // difference of the states of two consecutive ones is the increment
460 // (mix() is a bijection, which unmix() undoes by its definition)
461 ASSERT_EQ(0x94d049bb133111ebULL * 0x319642b2d24d8ec3ULL, 1u);
462 ASSERT_EQ(0xbf58476d1ce4e5b9ULL * 0x96de1b173f119089ULL, 1u);
463 for (const Reference & reference : references)
464 ASSERT_EQ(unmix(reference.numbers[0]), reference.seed + SplitMix64_Rng::increment);
465
466 std::uint64_t before = unmix(random_detail::seed_of_its_own());
467 for (int i = 0; i < 1000; ++i)
468 {
469 const std::uint64_t state = unmix(random_detail::seed_of_its_own());
470 ASSERT_EQ(state - before, SplitMix64_Rng::increment) << "at " << i;
471 before = state;
472 }
473}
474
475TEST(RandomDetail, WhatSeedsTheSplitMix64OfTheProcessChangesWithTheClocks)
476{
477 // asked again and again, it changes as soon as a clock ticks: so does
478 // it from a run of the program to another
479 const std::uint64_t first = random_detail::entropy_of_the_run();
480 bool changed = false;
481 for (long i = 0; i < 100000000L and not changed; ++i)
482 changed = random_detail::entropy_of_the_run() != first;
483 EXPECT_TRUE(changed);
484}
485
486TEST(RandomDetail, TheSplitMix64OfTheProcessDoesNotStartFromAFixedState)
487{
488 // were it seeded with 0, its state would be the increment times the
489 // few seeds that the program has taken; a right one is that near to 0
490 // once in 10^9 runs
491 const std::uint64_t inverse = 0xf1de83e19937733dULL; // of the increment, modulo 2^64
492 ASSERT_EQ(SplitMix64_Rng::increment * inverse, 1u);
493 const std::uint64_t steps_from_zero = unmix(random_detail::seed_of_its_own()) * inverse;
494 EXPECT_GT(steps_from_zero, std::uint64_t{1} << 32);
495}
496
497// ---------------------------------------------------------------------------
498// Random_Source
499// ---------------------------------------------------------------------------
500
501TEST(RandomSource, WhatCannotThrowIsNoexceptAndItIsNotCopied)
502{
503 static_assert(std::uniform_random_bit_generator<Random_Source>);
504 static_assert(Random_Source::min() == 0 and Random_Source::max() == 0xffffffffu);
505 static_assert(std::is_nothrow_default_constructible_v<Random_Source>);
506 static_assert((std::is_nothrow_constructible_v<Random_Source, std::uint64_t>));
507 static_assert(std::is_nothrow_move_constructible_v<Random_Source>);
508 static_assert(std::is_nothrow_move_assignable_v<Random_Source>);
509 static_assert(std::is_nothrow_swappable_v<Random_Source>);
510 static_assert(not std::is_copy_constructible_v<Random_Source>);
511 static_assert(not std::is_copy_assignable_v<Random_Source>);
512 // a seed alone is not taken for a source without saying so
513 static_assert(not std::is_convertible_v<std::uint64_t, Random_Source>);
514
516 static_assert(noexcept(s()) and noexcept(s.get()) and noexcept(s.uniform()));
517 static_assert(noexcept(s.uniform_int_ne(1)) and noexcept(s.swap(s)));
518 static_assert(noexcept(s.seed(1)) and noexcept(s.engine()));
519 // what may make the Mersenne Twister may throw std::bad_alloc, and
520 // uniform_int() rejects 0
521 static_assert(not noexcept(Random_Source(Engine::Mersenne_Twister, 1)));
522 static_assert(not noexcept(s.seed(Engine::Mersenne_Twister, 1)));
523 static_assert(not noexcept(s.mersenne_twister()));
524 static_assert(not noexcept(s.uniform_int(1)));
525
526 // a word for the state of SplitMix64, and a pointer
527 EXPECT_LE(sizeof(Random_Source), 2 * sizeof(std::uint64_t));
528}
529
530TEST(RandomSource, ByDefaultItDrawsFromASplitMix64WithASeedOfItsOwn)
531{
532 Random_Source a, b;
533 EXPECT_EQ(a.engine(), Engine::SplitMix64);
534 EXPECT_EQ(b.engine(), Engine::SplitMix64);
535 EXPECT_FALSE(a == b);
536
537 // each one takes a number of the SplitMix64 of the process as its
538 // seed, and one alone
539 for (int i = 0; i < 100; ++i)
540 {
541 const std::uint64_t seed = seed_of_the_next_source();
543 ASSERT_EQ(c, Random_Source(seed));
544 const std::uint64_t next = seed_of_the_next_source();
545 ASSERT_EQ(unmix(next) - unmix(seed), 2 * SplitMix64_Rng::increment);
546 }
547
548 // one built with a seed takes none
549 const std::uint64_t seed = seed_of_the_next_source();
550 Random_Source d(5), e(Engine::SplitMix64, 5), f(Engine::Mersenne_Twister, 5);
551 EXPECT_EQ(Random_Source(), Random_Source(seed));
552}
553
554TEST(RandomSource, EachOneBuiltByDefaultHasItsOwnNumbersFromEveryThread)
555{
556 // the first 64 bits of 16000 sources built by 8 threads at once: all
557 // different, unless two took the same seed (two right ones coincide
558 // once in 10^11 runs)
559 constexpr size_t threads = 8, each = 2000;
560 Array<std::uint64_t> firsts(threads * each, std::uint64_t{0});
561 {
562 std::thread pool[threads];
563 // joins those that started, also if starting another one throws
564 Init_Guard join_them([&pool]
565 {
566 for (std::thread & th : pool)
567 if (th.joinable())
568 th.join();
569 });
570 for (size_t t = 0; t < threads; ++t)
571 pool[t] = std::thread([&firsts, t]
572 {
573 for (size_t i = 0; i < each; ++i)
574 {
576 const std::uint64_t first = s();
577 firsts(t * each + i) = (first << 32) | s();
578 }
579 });
580 }
581
583 for (size_t i = 0; i < firsts.size(); ++i)
584 ASSERT_NE(seen.insert(firsts(i)), nullptr) << "source " << i << " repeats another";
585}
586
587TEST(RandomSource, TheFirstNumbersOfSourcesSeededAsByDefaultAreUniform)
588{
589 // A source built by default takes as its seed the next number of the
590 // SplitMix64 of the process (ByDefaultItDrawsFromASplitMix64WithASeedOfItsOwn
591 // checks it). Sources seeded so, from a fixed state, so that a failure
592 // can be reproduced: the high byte and the low byte of the first number
593 // of 64000 of them
594 const size_t n = 64000;
595 SplitMix64_Rng process(2026);
596 Array<size_t> high_bytes(256, size_t{0}), low_bytes(256, size_t{0});
597 for (size_t i = 0; i < n; ++i)
598 {
599 Random_Source s(process());
600 const std::uint32_t x = s();
601 ++high_bytes(x >> 24);
602 ++low_bytes(x & 0xff);
603 }
604 EXPECT_TRUE(are_uniform(high_bytes, n));
605 EXPECT_TRUE(are_uniform(low_bytes, n));
606}
607
608TEST(RandomSource, WithASeedAloneEveryDrawIsThatOfSplitMix64)
609{
610 // the draws of a source and those of a twin SplitMix64_Rng through the
611 // definition of each draw, in an order that another generator picks, so
612 // that a draw that took a number more or less would show in the next
613 for (const std::uint64_t seed : seeds)
614 for (const bool by_the_engine : {false, true})
615 {
616 Random_Source s = by_the_engine ? Random_Source(Engine::SplitMix64, seed)
618 ASSERT_EQ(s.engine(), Engine::SplitMix64);
619 SplitMix64_Rng twin(seed), pick(seed ^ 0x5eedULL);
620 for (int i = 0; i < 20000; ++i)
621 {
622 const std::uint64_t n = bounds[pick() % std::size(bounds)];
623 switch (pick() % 5)
624 {
625 case 0:
626 ASSERT_EQ(s(), high(twin)) << "seed " << seed << ", draw " << i;
627 break;
628 case 1:
629 ASSERT_EQ(s.get(), high(twin)) << "seed " << seed << ", draw " << i;
630 break;
631 case 2:
632 // the high 53 bits, divided by 2^53
633 ASSERT_EQ(s.uniform(), std::ldexp(static_cast<double>(twin() >> 11), -53))
634 << "seed " << seed << ", draw " << i;
635 break;
636 case 3:
637 ASSERT_EQ(s.uniform_int(n), lemire(twin, n))
638 << "seed " << seed << ", draw " << i << ", n " << n;
639 break;
640 default:
641 ASSERT_EQ(s.uniform_int_ne(n), lemire(twin, n))
642 << "seed " << seed << ", draw " << i << ", n " << n;
643 }
644 }
645 EXPECT_EQ(s.engine(), Engine::SplitMix64);
646 }
647}
648
649TEST(RandomSource, WithTheMersenneTwisterEveryDrawIsThatOfGslCompatibleRng)
650{
651 // as above, with a twin Gsl_Compatible_Rng, whose draws are those of GSL
652 for (const std::uint64_t seed : seeds)
653 {
654 Random_Source s(Engine::Mersenne_Twister, seed);
655 ASSERT_EQ(s.engine(), Engine::Mersenne_Twister);
656 Gsl_Compatible_Rng twin(static_cast<unsigned long>(seed));
657 SplitMix64_Rng pick(seed ^ 0x5eedULL);
658 for (int i = 0; i < 20000; ++i)
659 {
660 const auto n = static_cast<unsigned long>(bounds[pick() % std::size(bounds)]);
661 switch (pick() % 5)
662 {
663 case 0:
664 ASSERT_EQ(s(), twin()) << "seed " << seed << ", draw " << i;
665 break;
666 case 1:
667 ASSERT_EQ(s.get(), twin.get()) << "seed " << seed << ", draw " << i;
668 break;
669 case 2:
670 ASSERT_EQ(s.uniform(), twin.uniform()) << "seed " << seed << ", draw " << i;
671 break;
672 case 3:
673 ASSERT_EQ(s.uniform_int(n), twin.uniform_int(n))
674 << "seed " << seed << ", draw " << i << ", n " << n;
675 break;
676 default:
677 ASSERT_EQ(s.uniform_int_ne(n), twin.uniform_int(n))
678 << "seed " << seed << ", draw " << i << ", n " << n;
679 }
680 }
681 EXPECT_EQ(s.mersenne_twister(), twin);
682 }
683}
684
685TEST(RandomSource, TheNumbersOfTheMersenneTwisterAreThoseOfTheStandard)
686{
687 // the 10000th number of the Mersenne Twister with its default seed,
688 // which the standard gives for std::mt19937
689 Random_Source s(Engine::Mersenne_Twister, 5489);
690 std::uint32_t x = 0;
691 for (int i = 0; i < 10000; ++i)
692 x = s();
693 EXPECT_EQ(x, 4123659995u);
694}
695
696TEST(RandomSource, AnIntegerBelowABoundAboveThe32BitsIsANumberOf64BitsUnderAMask)
697{
698 for (const std::uint64_t seed : seeds)
699 {
700 Random_Source small(seed), big(Engine::Mersenne_Twister, seed);
701 SplitMix64_Rng small_twin(seed), pick(seed ^ 0x5eedULL);
702 Gsl_Compatible_Rng big_twin(static_cast<unsigned long>(seed));
703 for (int i = 0; i < 4000; ++i)
704 {
705 const std::uint64_t n = wide_bounds[pick() % std::size(wide_bounds)];
706 const std::uint64_t k = small.uniform_int(n);
707 ASSERT_LT(k, n);
708 ASSERT_EQ(k, under_a_mask([&small_twin] { return small_twin(); }, n))
709 << "seed " << seed << ", draw " << i << ", n " << n;
710 ASSERT_EQ(big.uniform_int_ne(n),
711 under_a_mask([&big_twin] { return two_words(big_twin); }, n))
712 << "seed " << seed << ", draw " << i << ", n " << n;
713 }
714 // neither took a number more or less
715 EXPECT_EQ(small(), high(small_twin));
716 EXPECT_EQ(big(), big_twin());
717 }
718}
719
720TEST(RandomSource, TheIntegersBelowABoundAreUniform)
721{
722 // with each engine: every integer below a small bound, and 64 ranges of
723 // the same length below a large one
724 const size_t draws = 200000;
725 for (const Engine engine : engines)
726 {
727 Random_Source s = source_of(engine, 2026);
728 for (const std::uint64_t n : {2ULL, 3ULL, 10ULL, 1000ULL})
729 {
730 Array<size_t> counts(n, size_t{0});
731 for (size_t i = 0; i < draws; ++i)
732 ++counts(s.uniform_int(n));
733 EXPECT_TRUE(are_uniform(counts, draws)) << name_of(engine) << ", n " << n;
734 }
735 for (const std::uint64_t n : {0xc0000000ULL, 0x100000000ULL, 0xc000000000ULL,
736 0xc000000000000000ULL})
737 {
738 Array<size_t> counts(64, size_t{0});
739 for (size_t i = 0; i < draws; ++i)
740 ++counts(s.uniform_int(n) / (n / 64));
741 EXPECT_TRUE(are_uniform(counts, draws)) << name_of(engine) << ", n " << n;
742 }
743 }
744}
745
746TEST(RandomSource, TheRealsAreUniformInTheUnitInterval)
747{
748 const size_t draws = 200000;
749 for (const Engine engine : engines)
750 {
751 Random_Source s = source_of(engine, 7);
752 Array<size_t> counts(100, size_t{0});
753 bool finer_than_32_bits = false;
754 for (size_t i = 0; i < draws; ++i)
755 {
756 const double u = s.uniform();
757 ASSERT_GE(u, 0.0);
758 ASSERT_LT(u, 1.0);
759 ++counts(static_cast<size_t>(u * 100));
760 const double scaled = std::ldexp(u, 32);
761 finer_than_32_bits = finer_than_32_bits or scaled != std::floor(scaled);
762 }
763 EXPECT_TRUE(are_uniform(counts, draws)) << name_of(engine);
764 // those of SplitMix64 have 53 bits; those of GSL, 32
765 EXPECT_EQ(finer_than_32_bits, engine == Engine::SplitMix64);
766 }
767}
768
769TEST(RandomSource, AnIntegerBelowZeroValuesIsRejectedAndTakesNoNumber)
770{
771 for (const Engine engine : engines)
772 {
773 Random_Source s = source_of(engine, 9);
774 EXPECT_THROW((void) s.uniform_int(0), std::domain_error) << name_of(engine);
775 EXPECT_EQ(s, source_of(engine, 9)) << name_of(engine);
776 }
777}
778
779TEST(RandomSource, TheIntegerBelowOneValueIsZero)
780{
781 for (const Engine engine : engines)
782 {
783 Random_Source s = source_of(engine, 9);
784 for (int i = 0; i < 1000; ++i)
785 {
786 ASSERT_EQ(s.uniform_int(1), 0u) << name_of(engine);
787 ASSERT_EQ(s.uniform_int_ne(1), 0u) << name_of(engine);
788 }
789 }
790}
791
792TEST(RandomSource, ASeedOfTheMersenneTwisterGivesTheSameNumbersOnEveryPlatform)
793{
794 // 0 is 4357, as for GSL; another seed, by its low 32 bits, 0 among them,
795 // as GSL takes an unsigned long of 64 bits. The numbers are those of
796 // std::mt19937 for that word, which the standard fixes: whatever the bits
797 // of unsigned long, which on Windows has 32 and made 4357 of a seed whose
798 // low 32 bits are 0
799 struct Case
800 {
801 std::uint64_t seed;
802 std::uint32_t word;
803 };
804 const Case cases[] = {{0, 4357}, {1, 1}, {4357, 4357}, {0xffffffffULL, 0xffffffffu},
805 {0x100000000ULL, 0}, {0x100000005ULL, 5},
806 {0xffffffff00000000ULL, 0}, {0xffffffffffffffffULL, 0xffffffffu}};
807 for (const Case & c : cases)
808 for (int how = 0; how < 3; ++how)
809 {
810 // built with it; seeded from SplitMix64; and seeded again where its
811 // Mersenne Twister is
812 Random_Source s = how == 0 ? Random_Source(Engine::Mersenne_Twister, c.seed)
813 : how == 1 ? Random_Source(9)
814 : Random_Source(Engine::Mersenne_Twister, 9);
815 const Gsl_Compatible_Rng * const before = how == 2 ? &s.mersenne_twister() : nullptr;
816 if (how != 0)
817 s.seed(Engine::Mersenne_Twister, c.seed);
818 ASSERT_EQ(s.engine(), Engine::Mersenne_Twister);
819 if (before != nullptr)
820 EXPECT_EQ(&s.mersenne_twister(), before);
821
822 std::mt19937 twin(c.word);
823 for (int i = 0; i < 1000; ++i)
824 ASSERT_EQ(s(), twin()) << "seed " << c.seed << ", way " << how << ", number " << i;
825 }
826}
827
828TEST(RandomSource, ASeedChoosesTheEngine)
829{
830 for (const std::uint64_t seed : seeds)
831 {
832 // from SplitMix64 to the Mersenne Twister, as built with it: GSL's on
833 // every platform, since no seed here but 0 has its low 32 bits at 0
835 s.seed(Engine::Mersenne_Twister, seed);
836 EXPECT_EQ(s.engine(), Engine::Mersenne_Twister);
837 EXPECT_EQ(s, Random_Source(Engine::Mersenne_Twister, seed));
838 EXPECT_EQ(s.mersenne_twister(), Gsl_Compatible_Rng(static_cast<unsigned long>(seed)));
839
840 // seeded again where it is: a reference to it sees the new seed. Its
841 // twister is not GSL's of seed + 1 where unsigned long has 32 bits:
842 // there 0xffffffff + 1 would be 0, which GSL takes for 4357
844 (void) s();
845 s.seed(Engine::Mersenne_Twister, seed + 1);
846 EXPECT_EQ(&s.mersenne_twister(), &mt);
847 EXPECT_EQ(mt, Random_Source(Engine::Mersenne_Twister, seed + 1).mersenne_twister());
848
849 // and back to SplitMix64, with a seed alone or with the engine
850 s.seed(seed);
851 EXPECT_EQ(s.engine(), Engine::SplitMix64);
852 EXPECT_EQ(s, Random_Source(seed));
853 s.seed(Engine::Mersenne_Twister, seed);
854 s.seed(Engine::SplitMix64, seed + 1);
855 EXPECT_EQ(s.engine(), Engine::SplitMix64);
856 EXPECT_EQ(s, Random_Source(seed + 1));
857
858 // on SplitMix64, a seed starts its numbers again
859 (void) s();
860 EXPECT_FALSE(s == Random_Source(seed + 1));
861 s.seed(seed + 1);
862 EXPECT_EQ(s, Random_Source(seed + 1));
863 }
864}
865
866TEST(RandomSource, ItsMersenneTwisterIsSeededWithTheNextNumberOfItsSplitMix64)
867{
868 for (const std::uint64_t seed : seeds)
869 {
871 SplitMix64_Rng twin(seed);
872 for (int i = 0; i < 3; ++i)
873 ASSERT_EQ(s(), high(twin));
874
876 EXPECT_EQ(s.engine(), Engine::Mersenne_Twister);
877 Gsl_Compatible_Rng mt_twin(high(twin));
878 EXPECT_EQ(mt, mt_twin);
879
880 // it is the generator of the source from then on, and one alone
881 EXPECT_EQ(&s.mersenne_twister(), &mt);
882 for (int i = 0; i < 100; ++i)
883 {
884 ASSERT_EQ(s(), mt_twin());
885 ASSERT_EQ(mt(), mt_twin()); // a draw through it is one of the source
886 ASSERT_EQ(s.uniform_int(1000), mt_twin.uniform_int(1000));
887 }
888 mt.seed(77); // and so is a seed given through it
889 EXPECT_EQ(s, Random_Source(Engine::Mersenne_Twister, 77));
890 }
891
892 // two sources built by default make different ones
893 Random_Source a, b;
894 EXPECT_FALSE(a.mersenne_twister() == b.mersenne_twister());
895}
896
897TEST(RandomSource, AMoveTakesTheGeneratorAndLeavesASplitMix64WithASeedOfItsOwn)
898{
899 for (const Engine engine : engines)
900 for (const bool by_assignment : {false, true})
901 for (const Engine engine_of_the_target : engines)
902 {
903 Random_Source from = source_of(engine, 5), twin = source_of(engine, 5);
904 for (int i = 0; i < 10; ++i)
905 ASSERT_EQ(from(), twin());
906 const Gsl_Compatible_Rng * const mt =
907 engine == Engine::Mersenne_Twister ? &from.mersenne_twister() : nullptr;
908
909 // the target: built from the source, or one of each engine that
910 // is assigned from it
911 Random_Source assigned = source_of(engine_of_the_target, 6);
912 std::optional<Random_Source> built;
913 const std::uint64_t seed_of_the_source_moved_from = seed_of_the_next_source();
914 if (by_assignment)
915 assigned = std::move(from);
916 else
917 built.emplace(std::move(from));
918 Random_Source & to = by_assignment ? assigned : *built;
919
920 // the target goes on with the numbers, with the same Mersenne
921 // Twister where it was
922 EXPECT_EQ(to.engine(), engine);
923 EXPECT_EQ(to, twin);
924 if (mt != nullptr)
925 EXPECT_EQ(&to.mersenne_twister(), mt);
926 for (int i = 0; i < 100; ++i)
927 ASSERT_EQ(to(), twin());
928
929 // the source is another one, of SplitMix64, with the seed that
930 // one built now would take, and it draws the numbers of that seed
931 EXPECT_EQ(from.engine(), Engine::SplitMix64);
932 EXPECT_FALSE(from == to);
933 Random_Source twin_of_the_source(seed_of_the_source_moved_from);
934 EXPECT_EQ(from, twin_of_the_source);
935 for (int i = 0; i < 100; ++i)
936 ASSERT_EQ(from.uniform_int(16), twin_of_the_source.uniform_int(16))
937 << "the source took the seed " << seed_of_the_source_moved_from;
938 }
939
940 // two sources moved from the same one, one after the other, differ
941 Random_Source a(5);
942 Random_Source b(std::move(a));
943 Random_Source c(std::move(b));
944 EXPECT_FALSE(a == b);
945 EXPECT_EQ(c, Random_Source(5));
946}
947
948TEST(RandomSource, AnAssignmentToItselfLeavesItAsItWas)
949{
950 for (const Engine engine : engines)
951 {
952 Random_Source s = source_of(engine, 5);
953 Random_Source & itself = s;
954 s = std::move(itself);
955 EXPECT_EQ(s, source_of(engine, 5)) << name_of(engine);
956 }
957}
958
959TEST(RandomSource, ASwapExchangesTheGeneratorsWithTheirStates)
960{
961 for (const Engine first : engines)
962 for (const Engine second : engines)
963 {
964 Random_Source a = source_of(first, 5), b = source_of(second, 6);
965 (void) a();
966 (void) b.uniform();
967 Random_Source a_twin = source_of(first, 5), b_twin = source_of(second, 6);
968 (void) a_twin();
969 (void) b_twin.uniform();
970
971 a.swap(b);
972 EXPECT_EQ(a.engine(), second);
973 EXPECT_EQ(b.engine(), first);
974 EXPECT_EQ(a, b_twin);
975 EXPECT_EQ(b, a_twin);
976
977 using std::swap;
978 swap(a, b); // that of the standard, by three moves
979 EXPECT_EQ(a, a_twin);
980 EXPECT_EQ(b, b_twin);
981 }
982}
983
984TEST(RandomSource, TwoAreEqualWhenTheyGiveTheSameNumbersFromNowOn)
985{
986 for (const Engine engine : engines)
987 {
988 Random_Source a = source_of(engine, 5), b = source_of(engine, 5);
989 EXPECT_TRUE(a == b);
990 (void) a();
991 EXPECT_FALSE(a == b);
992 (void) b();
993 EXPECT_TRUE(a == b);
994 EXPECT_FALSE(a == source_of(engine, 6));
995 }
996 // one of each engine with the same seed are not: their numbers differ
997 EXPECT_FALSE(Random_Source(5) == Random_Source(Engine::Mersenne_Twister, 5));
998 EXPECT_FALSE(Random_Source(Engine::Mersenne_Twister, 5) == Random_Source(5));
999 // 0 is 4357 for the Mersenne Twister, as for GSL, and the low 32 bits
1000 // of a seed count; SplitMix64 takes the 64
1001 EXPECT_TRUE(Random_Source(Engine::Mersenne_Twister, 0)
1002 == Random_Source(Engine::Mersenne_Twister, 4357));
1003 EXPECT_TRUE(Random_Source(Engine::Mersenne_Twister, 0x100000005ULL)
1004 == Random_Source(Engine::Mersenne_Twister, 5));
1005 EXPECT_FALSE(Random_Source(0x100000005ULL) == Random_Source(5));
1006}
1007
1008TEST(RandomSource, FeedsTheStandardLibrary)
1009{
1010 for (const Engine engine : engines)
1011 {
1012 Array<int> v(100, 0), w(100, 0);
1013 std::iota(v.begin(), v.end(), 0);
1014 std::iota(w.begin(), w.end(), 0);
1015 Random_Source a = source_of(engine, 5), b = source_of(engine, 5);
1016 std::shuffle(v.begin(), v.end(), a);
1017 std::shuffle(w.begin(), w.end(), b);
1018 EXPECT_TRUE(std::equal(v.begin(), v.end(), w.begin())) << name_of(engine);
1019 EXPECT_FALSE(std::is_sorted(v.begin(), v.end())) << name_of(engine);
1020
1021 std::uniform_real_distribution<double> d(0.0, 1.0);
1022 const double x = d(a);
1023 EXPECT_GE(x, 0.0);
1024 EXPECT_LT(x, 1.0);
1025 }
1026}
1027
1028#if ALEPH_WITH_GSL
1029TEST(RandomSource, ItsGslRngActsOnItsMersenneTwister)
1030{
1031 // a source on SplitMix64 makes its Mersenne Twister for it
1032 Random_Source s(5);
1033 SplitMix64_Rng twin(5);
1034 gsl_rng * r = s.gsl();
1035 ASSERT_NE(r, nullptr);
1036 EXPECT_EQ(s.engine(), Engine::Mersenne_Twister);
1037 EXPECT_EQ(gsl_rng_state(r), static_cast<void *>(&s.mersenne_twister()));
1038 EXPECT_EQ(s.gsl(), r); // made once
1039
1040 Gsl_Compatible_Rng mt_twin(high(twin));
1041 for (int i = 0; i < 100; ++i)
1042 {
1043 ASSERT_EQ(gsl_rng_get(r), mt_twin.get());
1044 ASSERT_EQ(s(), mt_twin());
1045 }
1046
1047 // it follows the generator through a move and a swap, and a seed of the
1048 // Mersenne Twister keeps it
1049 Random_Source moved(std::move(s));
1050 ASSERT_EQ(gsl_rng_get(r), mt_twin.get());
1051 EXPECT_EQ(moved.mersenne_twister(), mt_twin);
1052 Random_Source other;
1053 other.swap(moved);
1054 moved.seed(3);
1055 other.seed(Engine::Mersenne_Twister, 8);
1056 EXPECT_EQ(other.gsl(), r);
1057 EXPECT_EQ(gsl_rng_get(r), Gsl_Compatible_Rng(8).get());
1058}
1059#else
1060TEST(RandomSource, ItsGslRngActsOnItsMersenneTwister)
1061{
1062 GTEST_SKIP() << "without GSL (ALEPH_WITH_GSL is 0)";
1063}
1064#endif
The random number generators of Aleph: the one that gives the numbers of the gsl_rng_mt19937 of GSL,...
RAII guard for exception-safe resource cleanup.
long double w
Definition btreepic.C:153
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:141
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Definition tpl_array.H:450
Dynamic set backed by balanced binary search trees with automatic memory management.
Key * insert(const Key &key)
Inserts a key into the dynamic set.
A random number generator with the sequences of GSL's gsl_rng_mt19937.
Definition ah-random.H:147
void seed(const unsigned long s) noexcept
Seeds again, as gsl_rng_set(r, s).
Definition ah-random.H:277
unsigned long uniform_int(const unsigned long n)
An integer in [0, n), each with the same probability, as gsl_rng_uniform_int(r, n).
Definition ah-random.H:302
double uniform() noexcept
A real number in [0, 1), as gsl_rng_uniform(r).
Definition ah-random.H:334
unsigned long get() noexcept
The next number, as operator()().
Definition ah-random.H:289
RAII guard for graph algorithm initialization.
The source of random numbers of a randomized structure, which draws from one of two generators.
Definition ah-random.H:883
Engine
The generators that a source draws from.
Definition ah-random.H:938
static constexpr result_type max() noexcept
The greatest number of operator()(): 2^32 - 1.
Definition ah-random.H:947
unsigned long get() noexcept
The next number, as operator()().
Definition ah-random.H:1128
Gsl_Compatible_Rng & mersenne_twister()
The Mersenne Twister of the source, which is its engine from now on.
Definition ah-random.H:1087
double uniform() noexcept
A real number in [0, 1).
Definition ah-random.H:1181
Engine engine() const noexcept
The generator that the source draws from.
Definition ah-random.H:1028
void seed(const std::uint64_t s) noexcept
Draws from now on from the SplitMix64 of a seed.
Definition ah-random.H:1040
void swap(Random_Source &o) noexcept
Swaps the generators, with their states.
Definition ah-random.H:1021
std::uint64_t uniform_int(const std::uint64_t n)
An integer in [0, n), each with the same probability.
Definition ah-random.H:1167
static constexpr result_type min() noexcept
The least number of operator()().
Definition ah-random.H:944
std::uint64_t uniform_int_ne(const std::uint64_t n) noexcept
An integer in [0, n), each with the same probability, without checking n.
Definition ah-random.H:1147
SplitMix64: a random number generator whose state is a word of 64 bits.
Definition ah-random.H:642
static constexpr std::uint64_t increment
What the state advances for each number: 2^64 divided by the golden ratio, as an odd integer.
Definition ah-random.H:654
static constexpr std::uint64_t default_seed
The seed of a generator that is given none.
Definition ah-random.H:650
static constexpr result_type min() noexcept
The least number of operator()().
Definition ah-random.H:657
static constexpr result_type max() noexcept
The greatest number of operator()(): 2^64 - 1.
Definition ah-random.H:660
static constexpr std::uint64_t mix(std::uint64_t z) noexcept
The number of a state: a bijection of the 64-bit integers that scrambles its bits.
Definition ah-random.H:668
constexpr void seed(const std::uint64_t s) noexcept
Seeds again.
Definition ah-random.H:688
Minimal std::expected-style result type for C++20.
#define TEST(name)
std::uint64_t seed_of_its_own() noexcept
The seed of a Random_Source that is given none: the next number of a SplitMix64 of the process,...
Definition ah-random.H:836
std::uint64_t entropy_of_the_run() noexcept
A number that changes from a run of the program to another: the time, to the precision of the clock; ...
Definition ah-random.H:820
Main namespace for Aleph-w library functions.
void each(const size_t start, const size_t end, Op &op)
Execute an operation repeatedly over a range of indices.
T & swap(T &t1, T &t2)
Generic swap using object's swap method.
Definition ahTypes.H:123
and
Check uniqueness with explicit hash + equality functors.
T product(const Container &container, const T &init=T{1})
Compute product of all elements.
void next()
Advance all underlying iterators (bounds-checked).
Definition ah-zip.H:236
STL namespace.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
ValueArg< size_t > seed
Definition testHash.C:53
static int * k
static mt19937 engine
Gsl_Compatible_Rng r
Dynamic array container with automatic resizing.
Dynamic set implementations based on balanced binary search trees.