45#include <gtest/gtest.h>
48# include <gsl/gsl_randist.h>
49# include <gsl/gsl_rng.h>
70 void operator () (gsl_rng *
r)
const noexcept { gsl_rng_free(
r); }
78 constexpr bool same_log_as_gsl =
false;
80 constexpr bool same_log_as_gsl =
true;
83 ::testing::AssertionResult same_or_close(
const double ours,
const double gsl,
86 if (exactly ? ours == gsl
88 return ::testing::AssertionSuccess();
89 return ::testing::AssertionFailure() << std::setprecision(17) << ours <<
" is not "
100 ::testing::AssertionResult same_real(
const double ours,
const double gsl,
101 const bool takes_log =
false)
103#if defined(__x86_64__) || defined(_M_X64)
104 return same_or_close(ours, gsl, same_log_as_gsl or not takes_log);
106 return same_or_close(ours, gsl,
false);
113 ::testing::AssertionResult same_exponential(
const double ours,
const double gsl)
115 return same_or_close(ours, gsl, same_log_as_gsl);
118 using Gsl_Ptr = std::unique_ptr<gsl_rng, Gsl_Free>;
120 Gsl_Ptr make_gsl() {
return Gsl_Ptr(gsl_rng_alloc(gsl_rng_mt19937)); }
127 unsigned long get() {
return gsl_rng_get(
r); }
128 unsigned long uniform_int(
const unsigned long n) {
return gsl_rng_uniform_int(
r, n); }
129 double uniform() {
return gsl_rng_uniform(
r); }
130 double uniform_pos() {
return gsl_rng_uniform_pos(
r); }
135 double rounded(
const double product)
143 std::vector<unsigned long> seeds()
145 std::vector<unsigned long> s = {0, 1, 2, 4357, 5489, 12345, 0x7fffffffUL,
146 0xfffffffeUL, 0xffffffffUL, 1730000000UL};
147 if constexpr (
sizeof(
unsigned long) > 4)
149 s.push_back(
static_cast<unsigned long>(0x100000000ULL));
150 s.push_back(
static_cast<unsigned long>(0x123456789abcULL));
151 s.push_back(ULONG_MAX);
158 std::vector<unsigned long> bounds()
160 std::vector<unsigned long> b = {1, 2, 3, 5, 7, 10, 100, 1000, 1023, 1024, 1025,
161 65535, 65536, 1000003, 0x7fffffffUL, 0x80000000UL,
162 0x80000001UL, 0xfffffffeUL, 0xffffffffUL};
170 std::uint64_t
value = 0xcbf29ce484222325ULL;
172 void add_number(
const std::uint64_t x)
174 for (
int i = 0; i < 8; ++i)
176 value ^= (x >> (8 * i)) & 0xff;
177 value *= 0x100000001b3ULL;
181 void add_real(
const double x)
184 std::memcpy(&
bits, &x,
sizeof bits);
194 std::uint64_t hash_of_draws(
const unsigned long s, Rng && g)
196 const auto bs = bounds();
197 std::mt19937 order(
static_cast<std::uint32_t
>(s ^ 0x5a5a5a5aUL));
199 for (
int i = 0; i < 20000; ++i)
203 h.add_number(g.get());
206 h.add_number(g.uniform_int(bs[order() % bs.size()]));
209 h.add_real(g.uniform());
212 h.add_real(g.uniform_pos());
219TEST(GslCompatibleRng, TheDefaultSeedIsTheOneOfGsl)
223 Gsl_Ptr
r = make_gsl();
224 for (
int i = 0; i < 10000; ++i)
225 ASSERT_EQ(g.
get(), gsl_rng_get(
r.
get())) <<
"at " << i;
237TEST(GslCompatibleRng, TheDrawsOfGslByTheirHashes)
247 static const Golden golden[] = {
248 {0, 0x80a0475ca47c87a1ULL},
249 {1, 0x8f821e7e47e0b40bULL},
250 {4357, 0xc469a8ec8a18cd94ULL},
251 {12345, 0x6573fc224e03760dULL},
252 {0x7fffffffUL, 0x4d91b79879b89001ULL},
253 {0xffffffffUL, 0xc72e010949ebff2bULL},
255 for (
const Golden & x : golden)
258 <<
"seed " << x.seed;
260 Gsl_Ptr
r = make_gsl();
261 gsl_rng_set(
r.
get(), x.seed);
262 EXPECT_EQ(hash_of_draws(x.seed, Gsl_Draws{r.get()}), x.hash)
263 <<
"seed " << x.seed <<
", of GSL";
269TEST(GslCompatibleRng, EveryDrawIsTheOneOfGsl)
271 Gsl_Ptr
r = make_gsl();
272 const auto bs = bounds();
274 for (
unsigned long s : seeds())
276 gsl_rng_set(
r.
get(), s);
278 std::mt19937 order(
static_cast<std::uint32_t
>(s ^ 0x5a5a5a5aUL));
279 for (
int i = 0; i < 20000; ++i, ++draws)
283 ASSERT_EQ(g(), gsl_rng_get(
r.
get())) <<
"seed " << s <<
" at " << i;
286 ASSERT_EQ(g.get(), gsl_rng_get(
r.
get())) <<
"seed " << s <<
" at " << i;
290 const unsigned long n = bs[order() % bs.size()];
291 ASSERT_EQ(g.uniform_int(n), gsl_rng_uniform_int(
r.
get(), n))
292 <<
"seed " << s <<
" at " << i <<
", n = " << n;
296 ASSERT_EQ(g.uniform(), gsl_rng_uniform(
r.
get())) <<
"seed " << s <<
" at " << i;
299 ASSERT_EQ(g.uniform_pos(), gsl_rng_uniform_pos(
r.
get()))
300 <<
"seed " << s <<
" at " << i;
303 ASSERT_TRUE(same_real(g.flat(-3.5, 7.25), gsl_ran_flat(
r.
get(), -3.5, 7.25)))
304 <<
"seed " << s <<
" at " << i;
307 ASSERT_TRUE(same_real(g.gaussian(2.5), gsl_ran_gaussian(
r.
get(), 2.5),
true))
308 <<
"seed " << s <<
" at " << i;
311 ASSERT_TRUE(same_exponential(g.exponential(0.75),
312 gsl_ran_exponential(
r.
get(), 0.75)))
313 <<
"seed " << s <<
" at " << i;
317 EXPECT_EQ(draws, 20000 * seeds().
size());
324TEST(GslCompatibleRng, EveryDrawIsTheOneOfGsl)
326 GTEST_SKIP() <<
"without GSL (ALEPH_WITH_GSL is 0)";
330TEST(GslCompatibleRng, TheRealDrawsRoundEachProduct)
335 for (
unsigned long s : seeds())
338 for (
int i = 0; i < 2000; ++i)
340 const double a = 0.1, b = 1.7;
341 const double u = twin.
uniform();
342 ASSERT_EQ(g.flat(a, b), rounded(a * (1 - u)) + rounded(b * u))
343 <<
"seed " << s <<
" at " << i;
350 r2 = rounded(x * x) + rounded(
y *
y);
352 while (r2 > 1.0 or r2 == 0);
353 ASSERT_EQ(g.gaussian(2.5), 2.5 *
y * std::sqrt(-2.0 * std::log(r2) / r2))
354 <<
"seed " << s <<
" at " << i;
360TEST(GslCompatibleRng, SeedingAgainIsAsGslRngSet)
363 Gsl_Ptr
r = make_gsl();
366 for (
unsigned long s : seeds())
372 gsl_rng_set(
r.
get(), s);
373 for (
int i = 0; i < 1000; ++i)
374 ASSERT_EQ(g.
get(), gsl_rng_get(
r.
get())) <<
"seed " << s <<
" at " << i;
380 if constexpr (
sizeof(
unsigned long) > 4)
389TEST(GslCompatibleRng, TheNumbersOfTheStandard)
396 for (
int i = 0; i < 10000; ++i)
398 EXPECT_EQ(x, 4123659995u);
401TEST(GslCompatibleRng, TheNumbersAreThoseOfStdMt19937)
407 const auto mt_seed = [] (
const unsigned long x)
409 return x == 0 ? std::uint32_t(4357) :
static_cast<std::uint32_t
>(x & 0xffffffffUL);
411 for (
const unsigned long s : seeds())
414 std::mt19937 e(mt_seed(s));
415 for (
int i = 0; i < 3 * 624 + 7; ++i)
416 ASSERT_EQ(g(), e()) <<
"seed " << s <<
", number " << i;
418 e.seed(mt_seed(s + 1));
419 for (
int i = 0; i < 700; ++i)
420 ASSERT_EQ(g(), e()) <<
"seed " << s <<
" + 1, number " << i;
427TEST(GslCompatibleRng, UniformIntRejectsZeroAndTooLargeBounds)
430 EXPECT_THROW((
void) g.
uniform_int(0), std::domain_error);
431 if constexpr (
sizeof(
unsigned long) > 4)
433 EXPECT_THROW((
void) g.
uniform_int(
static_cast<unsigned long>(0x100000000ULL)),
438 for (
int i = 0; i < 1000; ++i)
445TEST(GslCompatibleRng, UniformIntIsItsDefinitionWhereTheQuotientReachesTheBound)
452 const unsigned long n = 92682;
453 const unsigned long scale = 0xffffffffUL / n;
455 size_t quotients_at_the_bound = 0, quotients_above_it = 0;
456 for (
int i = 0; i < 2000000; ++i)
458 unsigned long k = twin.
get() / scale;
461 ++(
k == n ? quotients_at_the_bound : quotients_above_it);
462 k = twin.
get() / scale;
464 ASSERT_EQ(checked.uniform_int(n),
k) <<
"draw " << i;
465 ASSERT_EQ(unchecked.uniform_int_ne(n),
k) <<
"draw " << i;
467 EXPECT_GT(quotients_at_the_bound, 5u);
468 EXPECT_GT(quotients_above_it, 5u);
469 EXPECT_EQ(checked, twin);
470 EXPECT_EQ(unchecked, twin);
471 static_assert(
noexcept(unchecked.uniform_int_ne(n))
and not
noexcept(checked.uniform_int(n)));
474TEST(GslCompatibleRng, UniformIntTakesAnotherNumberAfterTheBoundInANewState)
480 const unsigned long n = 92682;
481 const unsigned long scale = 0xffffffffUL / n;
484 unsigned long seed, draw;
486 for (
const Case & c : {Case{120249, 82262}, Case{131651, 64705}, Case{176754, 71349},
487 Case{194136, 85450}, Case{381252, 37245}})
490 ASSERT_EQ(twin.
get() / scale, n) <<
"seed " << c.seed;
491 ASSERT_EQ(twin.
get() / scale, c.draw) <<
"seed " << c.seed;
494 EXPECT_EQ(checked.uniform_int(n), c.draw) <<
"seed " << c.seed;
495 EXPECT_EQ(unchecked.
uniform_int_ne(n), c.draw) <<
"seed " << c.seed;
496 EXPECT_EQ(checked, twin);
497 EXPECT_EQ(unchecked, twin);
501TEST(GslCompatibleRng, RangesOfTheDraws)
504 for (
int i = 0; i < 100000; ++i)
512 const double f = g.
flat(-2.0, 3.0);
520TEST(GslCompatibleRng, MomentsOfTheDistributions)
525 const int n = 200000;
526 double s = 0, s2 = 0, e = 0, f = 0;
527 std::vector<int> counts(10, 0);
528 for (
int i = 0; i < n; ++i)
534 f += g.
flat(1.0, 5.0);
537 EXPECT_NEAR(s / n, 0.0, 0.03);
538 EXPECT_NEAR(s2 / n, 4.0, 0.05);
539 EXPECT_NEAR(e / n, 3.0, 0.03);
540 EXPECT_NEAR(f / n, 3.0, 0.02);
542 EXPECT_NEAR(c, n / 10, 600);
545TEST(GslCompatibleRng, ACopyGoesOnWithTheSameNumbers)
548 for (
int i = 0; i < 100; ++i)
553 for (
int i = 0; i < 1000; ++i)
557 EXPECT_FALSE(c == g);
560TEST(GslCompatibleRng, FeedsTheStandardLibrary)
562 static_assert(std::uniform_random_bit_generator<Gsl_Compatible_Rng>);
564 std::vector<int> v(100);
565 std::iota(v.begin(), v.end(), 0);
568 std::shuffle(v.begin(), v.end(), a);
569 std::shuffle(
w.begin(),
w.end(), b);
571 EXPECT_TRUE(std::is_permutation(v.begin(), v.end(),
w.begin()));
573 std::uniform_real_distribution<double> d(0.0, 1.0);
574 const double x = d(a);
The random number generators of Aleph: the one that gives the numbers of the gsl_rng_mt19937 of GSL,...
size_t size_t int32_t value
A random number generator with the sequences of GSL's gsl_rng_mt19937.
double uniform_pos() noexcept
A real number in (0, 1), as gsl_rng_uniform_pos(r).
static constexpr result_type max() noexcept
The greatest number of operator()(): 2^32 - 1.
static constexpr result_type min() noexcept
The least number of operator()().
void seed(const unsigned long s) noexcept
Seeds again, as gsl_rng_set(r, s).
double gaussian(const double sigma) noexcept
A real number of the normal distribution of mean 0, as gsl_ran_gaussian(r, sigma).
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).
double exponential(const double mu) noexcept
A real number of the exponential distribution, as gsl_ran_exponential(r, mu).
double flat(const double a, const double b) noexcept
A real number of the uniform distribution on [a, b), as gsl_ran_flat(r, a, b).
unsigned long get() noexcept
The next number, as operator()().
std::uint32_t result_type
The type of the numbers of operator()().
unsigned long uniform_int_ne(const unsigned long n) noexcept
The integer of uniform_int(), without checking n.
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_max_function > > max(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_abs_function > > abs(const __gmp_expr< T, U > &expr)
std::size_t uniform_int(Engine &eng, const std::size_t lo, const std::size_t hi)
Sample a uniform integer in [lo, hi] (inclusive).
Main namespace for Aleph-w library functions.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
constexpr Bit_Column bits(const unsigned n)
Column of n bits without sign: the integers of [0, 2^n - 1].
and
Check uniqueness with explicit hash + equality functors.
T product(const Container &container, const T &init=T{1})
Compute product of all elements.