46#include <gtest/gtest.h>
75 std::uint64_t numbers[5];
78 constexpr Reference references[] = {
80 {0xe220a8397b1dcdafULL, 0x6e789e6aa1b965f4ULL, 0x06c45d188009454fULL,
81 0xf88bb8a8724c81ecULL, 0x1b39896a51a8749bULL}},
83 {0x910a2dec89025cc1ULL, 0xbeeb8da1658eec67ULL, 0xf893a2eefb32555eULL,
84 0x71c18690ee42c90bULL, 0x71bb54d8d101b5b9ULL}},
86 {0x599ed017fb08fc85ULL, 0x2c73f08458540fa5ULL, 0x883ebce5a3f27c77ULL,
87 0x3fbef740e9177b3fULL, 0xe3b8346708cb5ecdULL}},
88 {0xffffffffffffffffULL,
89 {0xe4d971771b652c20ULL, 0xe99ff867dbf682c9ULL, 0x382ff84cb27281e9ULL,
90 0x6d1db36ccba982d2ULL, 0xb4a0472e578069aeULL}},
95 constexpr std::uint64_t seeds[] = {0ULL, 1ULL, 12345ULL, 0xfffffffeULL, 0xffffffffULL,
96 0x100000005ULL, 0xffffffffffffffffULL};
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};
107 constexpr std::uint64_t wide_bounds[] = {0x100000000ULL, 0x100000001ULL, 0x1ffffffffULL,
108 0x200000000ULL, 0x10000003039ULL,
109 0x8000000000000000ULL, 0x8000000000000001ULL,
110 0xffffffffffffffffULL};
115 return static_cast<std::uint32_t
>(twin() >> 32);
122 std::uint64_t lemire(
SplitMix64_Rng & twin,
const std::uint64_t n)
124 const std::uint64_t rejected = (std::uint64_t{1} << 32) % n;
127 const std::uint64_t
product = high(twin) * n;
128 if ((
product & 0xffffffffULL) >= rejected)
134 std::uint64_t mask_for(
const std::uint64_t n)
136 std::uint64_t mask = 0;
138 mask = (mask << 1) | 1;
144 template <
class Next64>
145 std::uint64_t under_a_mask(Next64 next64,
const std::uint64_t n)
147 const std::uint64_t mask = mask_for(n);
149 if (
const std::uint64_t
k = next64() & mask;
k < n)
157 const std::uint64_t first = twin();
158 const std::uint64_t second = twin();
159 return (first << 32) | second;
169 size_t without_a_number = 0;
170 bool in_range =
true;
173 template <
class Word,
class Method>
174 Tally tally(Method method,
const Word n)
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)
183 std::uint32_t later = 0;
184 auto next = [&calls, &later, first] () -> Word
186 return static_cast<Word
>(calls++ == 0 ? first : later++);
188 const Word
k = method(
next, n);
195 ++t.without_a_number;
204 template <
class Word,
class W
ide>
205 void expect_that_bounded_is_exact(
const Word n)
207 const size_t words =
size_t{1} << std::numeric_limits<Word>::digits;
208 const Tally t = tally<Word>([] (
auto &
next,
const Word
m)
210 return random_detail::bounded<Word, Wide>(
next,
m);
212 ASSERT_TRUE(t.in_range) <<
"n = " << +n;
213 EXPECT_EQ(t.without_a_number, 0u) <<
"n = " << +n;
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;
220 template <
class Word>
221 void expect_that_masked_is_exact(
const Word n)
223 const size_t words =
size_t{1} << std::numeric_limits<Word>::digits;
224 const Tally t = tally<Word>([] (
auto &
next,
const Word
m)
226 return random_detail::masked<Word>(
next,
m);
228 ASSERT_TRUE(t.in_range) <<
"n = " << +n;
231 EXPECT_EQ(t.without_a_number, words);
235 size_t under_the_mask = 1;
236 while (under_the_mask < n)
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;
241 for (
size_t k = 0;
k < n; ++
k)
242 ASSERT_EQ(t.kept(
k), words / under_the_mask) <<
"n = " << +n <<
", integer " <<
k;
250 ::testing::AssertionResult are_uniform(
const Array<size_t> & counts,
const size_t draws)
252 const double expected =
static_cast<double>(draws) /
static_cast<double>(counts.
size());
254 for (
size_t i = 0; i < counts.
size(); ++i)
256 const double d =
static_cast<double>(counts(i)) -
expected;
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";
271 std::uint64_t unmix(std::uint64_t z)
273 z = z ^ (z >> 31) ^ (z >> 62);
274 z *= 0x319642b2d24d8ec3ULL;
275 z = z ^ (z >> 27) ^ (z >> 54);
276 z *= 0x96de1b173f119089ULL;
277 return z ^ (z >> 30) ^ (z >> 60);
283 std::uint64_t seed_of_the_next_source()
295 constexpr Engine engines[] = {Engine::SplitMix64, Engine::Mersenne_Twister};
297 const char * name_of(
const Engine
engine)
299 return engine == Engine::SplitMix64 ?
"SplitMix64" :
"Mersenne Twister";
307TEST(SplitMix64Rng, TheNumbersOfItsReference)
309 for (
const Reference & reference : references)
312 for (
const std::uint64_t number : reference.numbers)
313 EXPECT_EQ(g(), number) <<
"seed " << reference.seed;
317TEST(SplitMix64Rng, TheNumberIsTheMixOfTheStateAdvancedByTheIncrement)
320 "an odd increment goes through the 2^64 states");
321 for (
const Reference & reference : references)
323 std::uint64_t state = reference.seed;
324 for (
const std::uint64_t number : reference.numbers)
330TEST(SplitMix64Rng, IsAConstantExpression)
333 constexpr auto third = [] (
const std::uint64_t
seed)
341 static_assert(third(0) == 0x06c45d188009454fULL);
342 static_assert(third(1234567) == 0x883ebce5a3f27c77ULL);
346 static_assert(
noexcept(std::declval<SplitMix64_Rng &>()()));
350TEST(SplitMix64Rng, TheDefaultSeedIsZero)
353 for (
int i = 0; i < 100; ++i)
354 ASSERT_EQ(g(), zero());
357TEST(SplitMix64Rng, SeedingAgainIsAsBuildingWithTheSeed)
360 for (
int i = 0; i < 100; ++i)
363 for (
const Reference & reference : references)
365 g.
seed(reference.seed);
367 for (
const std::uint64_t number : reference.numbers)
368 EXPECT_EQ(g(), number) <<
"seed " << reference.seed;
372TEST(SplitMix64Rng, ACopyGoesOnWithTheSameNumbers)
375 for (
int i = 0; i < 100; ++i)
380 for (
int i = 0; i < 1000; ++i)
384 EXPECT_FALSE(c == g);
387TEST(SplitMix64Rng, FeedsTheStandardLibrary)
389 static_assert(std::uniform_random_bit_generator<SplitMix64_Rng>);
391 static_assert(std::is_trivially_copyable_v<SplitMix64_Rng>);
394 std::iota(v.begin(), v.end(), 0);
395 std::iota(
w.begin(),
w.end(), 0);
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()));
403TEST(SplitMix64Rng, ItsBitsAreBalanced)
407 const int n = 100000;
408 for (
const bool consecutive_seeds : {
false,
true})
412 for (
int i = 0; i < n; ++i)
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;
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" :
"");
428TEST(RandomDetail, BoundedIsExactWithEveryBoundOfWordsOf8Bits)
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));
434TEST(RandomDetail, BoundedIsExactWithWordsOf16Bits)
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));
443TEST(RandomDetail, MaskedIsExactWithEveryBoundOfWordsOf8Bits)
445 for (
unsigned n = 1; n < 256; ++n)
446 expect_that_masked_is_exact<std::uint8_t>(
static_cast<std::uint8_t
>(n));
449TEST(RandomDetail, MaskedIsExactWithWordsOf16Bits)
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));
456TEST(RandomDetail, TheSeedsOfTheirOwnAreNumbersOfOneSplitMix64)
461 ASSERT_EQ(0x94d049bb133111ebULL * 0x319642b2d24d8ec3ULL, 1u);
462 ASSERT_EQ(0xbf58476d1ce4e5b9ULL * 0x96de1b173f119089ULL, 1u);
463 for (
const Reference & reference : references)
467 for (
int i = 0; i < 1000; ++i)
475TEST(RandomDetail, WhatSeedsTheSplitMix64OfTheProcessChangesWithTheClocks)
480 bool changed =
false;
481 for (
long i = 0; i < 100000000L
and not changed; ++i)
483 EXPECT_TRUE(changed);
486TEST(RandomDetail, TheSplitMix64OfTheProcessDoesNotStartFromAFixedState)
491 const std::uint64_t inverse = 0xf1de83e19937733dULL;
494 EXPECT_GT(steps_from_zero, std::uint64_t{1} << 32);
501TEST(RandomSource, WhatCannotThrowIsNoexceptAndItIsNotCopied)
503 static_assert(std::uniform_random_bit_generator<Random_Source>);
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>);
513 static_assert(not std::is_convertible_v<std::uint64_t, Random_Source>);
516 static_assert(
noexcept(s())
and noexcept(s.
get())
and noexcept(s.
uniform()));
521 static_assert(not
noexcept(
Random_Source(Engine::Mersenne_Twister, 1)));
522 static_assert(not
noexcept(s.
seed(Engine::Mersenne_Twister, 1)));
530TEST(RandomSource, ByDefaultItDrawsFromASplitMix64WithASeedOfItsOwn)
533 EXPECT_EQ(a.
engine(), Engine::SplitMix64);
534 EXPECT_EQ(b.
engine(), Engine::SplitMix64);
535 EXPECT_FALSE(a == b);
539 for (
int i = 0; i < 100; ++i)
541 const std::uint64_t
seed = seed_of_the_next_source();
544 const std::uint64_t
next = seed_of_the_next_source();
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);
554TEST(RandomSource, EachOneBuiltByDefaultHasItsOwnNumbersFromEveryThread)
559 constexpr size_t threads = 8,
each = 2000;
562 std::thread pool[threads];
566 for (std::thread & th : pool)
570 for (
size_t t = 0; t < threads; ++t)
571 pool[t] = std::thread([&firsts, t]
573 for (
size_t i = 0; i <
each; ++i)
576 const std::uint64_t first = s();
577 firsts(t *
each + i) = (first << 32) | s();
583 for (
size_t i = 0; i < firsts.size(); ++i)
584 ASSERT_NE(seen.
insert(firsts(i)),
nullptr) <<
"source " << i <<
" repeats another";
587TEST(RandomSource, TheFirstNumbersOfSourcesSeededAsByDefaultAreUniform)
594 const size_t n = 64000;
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)
600 const std::uint32_t x = s();
601 ++high_bytes(x >> 24);
602 ++low_bytes(x & 0xff);
604 EXPECT_TRUE(are_uniform(high_bytes, n));
605 EXPECT_TRUE(are_uniform(low_bytes, n));
608TEST(RandomSource, WithASeedAloneEveryDrawIsThatOfSplitMix64)
613 for (
const std::uint64_t
seed : seeds)
614 for (
const bool by_the_engine : {
false,
true})
618 ASSERT_EQ(s.
engine(), Engine::SplitMix64);
620 for (
int i = 0; i < 20000; ++i)
622 const std::uint64_t n = bounds[pick() % std::size(bounds)];
626 ASSERT_EQ(s(), high(twin)) <<
"seed " <<
seed <<
", draw " << i;
629 ASSERT_EQ(s.
get(), high(twin)) <<
"seed " <<
seed <<
", draw " << i;
633 ASSERT_EQ(s.
uniform(), std::ldexp(
static_cast<double>(twin() >> 11), -53))
634 <<
"seed " <<
seed <<
", draw " << i;
638 <<
"seed " <<
seed <<
", draw " << i <<
", n " << n;
642 <<
"seed " <<
seed <<
", draw " << i <<
", n " << n;
645 EXPECT_EQ(s.
engine(), Engine::SplitMix64);
649TEST(RandomSource, WithTheMersenneTwisterEveryDrawIsThatOfGslCompatibleRng)
652 for (
const std::uint64_t
seed : seeds)
655 ASSERT_EQ(s.
engine(), Engine::Mersenne_Twister);
658 for (
int i = 0; i < 20000; ++i)
660 const auto n =
static_cast<unsigned long>(bounds[pick() % std::size(bounds)]);
664 ASSERT_EQ(s(), twin()) <<
"seed " <<
seed <<
", draw " << i;
667 ASSERT_EQ(s.
get(), twin.
get()) <<
"seed " <<
seed <<
", draw " << i;
674 <<
"seed " <<
seed <<
", draw " << i <<
", n " << n;
678 <<
"seed " <<
seed <<
", draw " << i <<
", n " << n;
685TEST(RandomSource, TheNumbersOfTheMersenneTwisterAreThoseOfTheStandard)
691 for (
int i = 0; i < 10000; ++i)
693 EXPECT_EQ(x, 4123659995u);
696TEST(RandomSource, AnIntegerBelowABoundAboveThe32BitsIsANumberOf64BitsUnderAMask)
698 for (
const std::uint64_t
seed : seeds)
703 for (
int i = 0; i < 4000; ++i)
705 const std::uint64_t n = wide_bounds[pick() % std::size(wide_bounds)];
706 const std::uint64_t
k = small.uniform_int(n);
708 ASSERT_EQ(
k, under_a_mask([&small_twin] {
return small_twin(); }, n))
709 <<
"seed " <<
seed <<
", draw " << i <<
", n " << n;
711 under_a_mask([&big_twin] {
return two_words(big_twin); }, n))
712 <<
"seed " <<
seed <<
", draw " << i <<
", n " << n;
715 EXPECT_EQ(small(), high(small_twin));
716 EXPECT_EQ(big(), big_twin());
720TEST(RandomSource, TheIntegersBelowABoundAreUniform)
724 const size_t draws = 200000;
725 for (
const Engine
engine : engines)
728 for (
const std::uint64_t n : {2ULL, 3ULL, 10ULL, 1000ULL})
731 for (
size_t i = 0; i < draws; ++i)
733 EXPECT_TRUE(are_uniform(counts, draws)) << name_of(
engine) <<
", n " << n;
735 for (
const std::uint64_t n : {0xc0000000ULL, 0x100000000ULL, 0xc000000000ULL,
736 0xc000000000000000ULL})
739 for (
size_t i = 0; i < draws; ++i)
741 EXPECT_TRUE(are_uniform(counts, draws)) << name_of(
engine) <<
", n " << n;
746TEST(RandomSource, TheRealsAreUniformInTheUnitInterval)
748 const size_t draws = 200000;
749 for (
const Engine
engine : engines)
753 bool finer_than_32_bits =
false;
754 for (
size_t i = 0; i < draws; ++i)
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);
763 EXPECT_TRUE(are_uniform(counts, draws)) << name_of(
engine);
765 EXPECT_EQ(finer_than_32_bits,
engine == Engine::SplitMix64);
769TEST(RandomSource, AnIntegerBelowZeroValuesIsRejectedAndTakesNoNumber)
771 for (
const Engine
engine : engines)
779TEST(RandomSource, TheIntegerBelowOneValueIsZero)
781 for (
const Engine
engine : engines)
784 for (
int i = 0; i < 1000; ++i)
792TEST(RandomSource, ASeedOfTheMersenneTwisterGivesTheSameNumbersOnEveryPlatform)
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)
817 s.
seed(Engine::Mersenne_Twister, c.seed);
818 ASSERT_EQ(s.
engine(), Engine::Mersenne_Twister);
819 if (before !=
nullptr)
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;
828TEST(RandomSource, ASeedChoosesTheEngine)
830 for (
const std::uint64_t
seed : seeds)
835 s.
seed(Engine::Mersenne_Twister,
seed);
836 EXPECT_EQ(s.
engine(), Engine::Mersenne_Twister);
845 s.
seed(Engine::Mersenne_Twister,
seed + 1);
847 EXPECT_EQ(mt,
Random_Source(Engine::Mersenne_Twister,
seed + 1).mersenne_twister());
851 EXPECT_EQ(s.
engine(), Engine::SplitMix64);
853 s.
seed(Engine::Mersenne_Twister,
seed);
854 s.
seed(Engine::SplitMix64,
seed + 1);
855 EXPECT_EQ(s.
engine(), Engine::SplitMix64);
866TEST(RandomSource, ItsMersenneTwisterIsSeededWithTheNextNumberOfItsSplitMix64)
868 for (
const std::uint64_t
seed : seeds)
872 for (
int i = 0; i < 3; ++i)
873 ASSERT_EQ(s(), high(twin));
876 EXPECT_EQ(s.
engine(), Engine::Mersenne_Twister);
878 EXPECT_EQ(mt, mt_twin);
882 for (
int i = 0; i < 100; ++i)
884 ASSERT_EQ(s(), mt_twin());
885 ASSERT_EQ(mt(), mt_twin());
897TEST(RandomSource, AMoveTakesTheGeneratorAndLeavesASplitMix64WithASeedOfItsOwn)
899 for (
const Engine
engine : engines)
900 for (
const bool by_assignment : {
false,
true})
901 for (
const Engine engine_of_the_target : engines)
904 for (
int i = 0; i < 10; ++i)
905 ASSERT_EQ(from(), twin());
912 std::optional<Random_Source> built;
913 const std::uint64_t seed_of_the_source_moved_from = seed_of_the_next_source();
915 assigned = std::move(from);
917 built.emplace(std::move(from));
926 for (
int i = 0; i < 100; ++i)
927 ASSERT_EQ(to(), twin());
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)
937 <<
"the source took the seed " << seed_of_the_source_moved_from;
944 EXPECT_FALSE(a == b);
948TEST(RandomSource, AnAssignmentToItselfLeavesItAsItWas)
950 for (
const Engine
engine : engines)
954 s = std::move(itself);
959TEST(RandomSource, ASwapExchangesTheGeneratorsWithTheirStates)
961 for (
const Engine first : engines)
962 for (
const Engine second : engines)
964 Random_Source a = source_of(first, 5), b = source_of(second, 6);
967 Random_Source a_twin = source_of(first, 5), b_twin = source_of(second, 6);
972 EXPECT_EQ(a.
engine(), second);
973 EXPECT_EQ(b.engine(), first);
974 EXPECT_EQ(a, b_twin);
975 EXPECT_EQ(b, a_twin);
979 EXPECT_EQ(a, a_twin);
980 EXPECT_EQ(b, b_twin);
984TEST(RandomSource, TwoAreEqualWhenTheyGiveTheSameNumbersFromNowOn)
986 for (
const Engine
engine : engines)
991 EXPECT_FALSE(a == b);
994 EXPECT_FALSE(a == source_of(
engine, 6));
1003 EXPECT_TRUE(
Random_Source(Engine::Mersenne_Twister, 0x100000005ULL)
1008TEST(RandomSource, FeedsTheStandardLibrary)
1010 for (
const Engine
engine : engines)
1013 std::iota(v.begin(), v.end(), 0);
1014 std::iota(
w.begin(),
w.end(), 0);
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);
1021 std::uniform_real_distribution<double> d(0.0, 1.0);
1022 const double x = d(a);
1029TEST(RandomSource, ItsGslRngActsOnItsMersenneTwister)
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);
1041 for (
int i = 0; i < 100; ++i)
1043 ASSERT_EQ(gsl_rng_get(
r), mt_twin.get());
1044 ASSERT_EQ(s(), mt_twin());
1050 ASSERT_EQ(gsl_rng_get(
r), mt_twin.get());
1051 EXPECT_EQ(moved.mersenne_twister(), mt_twin);
1055 other.
seed(Engine::Mersenne_Twister, 8);
1056 EXPECT_EQ(other.gsl(),
r);
1060TEST(RandomSource, ItsGslRngActsOnItsMersenneTwister)
1062 GTEST_SKIP() <<
"without GSL (ALEPH_WITH_GSL is 0)";
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.
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
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.
void seed(const unsigned long s) noexcept
Seeds again, as gsl_rng_set(r, s).
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).
double uniform() noexcept
A real number in [0, 1), as gsl_rng_uniform(r).
unsigned long get() noexcept
The next number, as operator()().
RAII guard for graph algorithm initialization.
The source of random numbers of a randomized structure, which draws from one of two generators.
Engine
The generators that a source draws from.
static constexpr result_type max() noexcept
The greatest number of operator()(): 2^32 - 1.
unsigned long get() noexcept
The next number, as operator()().
Gsl_Compatible_Rng & mersenne_twister()
The Mersenne Twister of the source, which is its engine from now on.
double uniform() noexcept
A real number in [0, 1).
Engine engine() const noexcept
The generator that the source draws from.
void seed(const std::uint64_t s) noexcept
Draws from now on from the SplitMix64 of a seed.
void swap(Random_Source &o) noexcept
Swaps the generators, with their states.
std::uint64_t uniform_int(const std::uint64_t n)
An integer in [0, n), each with the same probability.
static constexpr result_type min() noexcept
The least number of operator()().
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.
SplitMix64: a random number generator whose state is a word of 64 bits.
static constexpr std::uint64_t increment
What the state advances for each number: 2^64 divided by the golden ratio, as an odd integer.
static constexpr std::uint64_t default_seed
The seed of a generator that is given none.
static constexpr result_type min() noexcept
The least number of operator()().
static constexpr result_type max() noexcept
The greatest number of operator()(): 2^64 - 1.
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.
constexpr void seed(const std::uint64_t s) noexcept
Seeds again.
Minimal std::expected-style result type for C++20.
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,...
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; ...
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.
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).
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Dynamic array container with automatic resizing.
Dynamic set implementations based on balanced binary search trees.