87# include <type_traits>
102# ifndef ALEPH_WITH_GSL
103# if __has_include(<gsl/gsl_rng.h>)
104# define ALEPH_WITH_GSL 1
106# define ALEPH_WITH_GSL 0
111# include <gsl/gsl_rng.h>
162 void init(
const std::uint32_t s)
noexcept
166 mt[i] = 1812433253u * (
mt[i - 1] ^ (
mt[i - 1] >> 30)) +
static_cast<std::uint32_t
>(i);
174 constexpr std::uint32_t upper = 0x80000000u, lower = 0x7fffffffu;
175 constexpr std::uint32_t matrix = 0x9908b0dfu;
179 const std::uint32_t
y = (
mt[
k] & upper) | (
mt[
k + 1] & lower);
184 const std::uint32_t
y = (
mt[
k] & upper) | (
mt[
k + 1] & lower);
187 const std::uint32_t
y = (
mt[
state_size - 1] & upper) | (
mt[0] & lower);
196 y ^= (
y << 7) & 0x9d2c5680u;
197 y ^= (
y << 15) & 0xefc60000u;
214 const unsigned long scale)
noexcept
218 k =
static_cast<unsigned long>(
next()) / scale;
228 return s == 0 ?
default_seed :
static_cast<std::uint32_t
>(s & 0xffffffffUL);
233 [[noreturn, gnu::noinline, gnu::cold]]
static void throw_bad_range(
const unsigned long n)
236 <<
", outside [1, " <<
max() <<
"]";
259 [[nodiscard]]
static constexpr result_type max() noexcept {
return 0xffffffffu; }
289 unsigned long get() noexcept {
return next(); }
304 if (n == 0 or n >
max()) [[unlikely]]
321 const unsigned long scale =
static_cast<unsigned long>(
max()) / n;
323 if (
const unsigned long k =
static_cast<unsigned long>(
tempered(
mt[
mti++])) / scale;
358 double flat(
const double a,
const double b)
noexcept
384 while (r2 > 1.0 or r2 == 0);
386 return sigma *
y * std::sqrt(-2.0 * std::log(r2) / r2);
397 return -mu * std::log1p(-
uniform());
412 static_assert(std::uniform_random_bit_generator<Gsl_Compatible_Rng>);
420 inline void aleph_gsl_compatible_rng_set(
void * state,
unsigned long s)
422 static_cast<Gsl_Compatible_Rng *
>(state)->
seed(s);
425 inline unsigned long aleph_gsl_compatible_rng_get(
void * state)
427 return static_cast<Gsl_Compatible_Rng *
>(state)->get();
430 inline double aleph_gsl_compatible_rng_get_double(
void * state)
432 return static_cast<Gsl_Compatible_Rng *
>(state)->uniform();
436 static_assert(std::is_trivially_copyable_v<Gsl_Compatible_Rng>
437 and std::is_trivially_destructible_v<Gsl_Compatible_Rng>,
438 "GSL copies the state of a gsl_rng by its bytes");
440 namespace random_detail
446 inline const gsl_rng_type gsl_compatible_rng_type =
448 "aleph_gsl_compatible_rng",
451 sizeof(Gsl_Compatible_Rng),
452 &aleph_gsl_compatible_rng_set,
453 &aleph_gsl_compatible_rng_get,
454 &aleph_gsl_compatible_rng_get_double
461 inline gsl_rng * make_gsl_adapter(Gsl_Compatible_Rng &
rng)
463 auto *
r =
static_cast<gsl_rng *
>(std::malloc(
sizeof(gsl_rng)));
465 throw std::bad_alloc();
466 r->type = &gsl_compatible_rng_type;
471 inline void free_gsl_adapter(
void *
r)
noexcept
501 using Free = void (*)(
void *)
noexcept;
507 explicit Block(
const unsigned long s) noexcept :
rng(s) {}
511 if (
void * a =
adapter.load(std::memory_order_acquire); a !=
nullptr)
530 std::unique_ptr<Block, Free_Block>
block;
545 void * mem = std::malloc(
sizeof(
Block));
547 throw std::bad_alloc();
580 return block ==
nullptr ? nullptr : &
block->rng;
598 [[nodiscard]] gsl_rng * gsl()
const
600 void * a =
block->adapter.load(std::memory_order_acquire);
603 gsl_rng * made = random_detail::make_gsl_adapter(
block->rng);
604 block->free_adapter.store(&random_detail::free_gsl_adapter, std::memory_order_relaxed);
605 if (
block->adapter.compare_exchange_strong(a, made, std::memory_order_acq_rel,
606 std::memory_order_acquire))
609 random_detail::free_gsl_adapter(made);
611 return static_cast<gsl_rng *
>(a);
616 static_assert(
sizeof(Gsl_Compatible_Rng_Owner) ==
sizeof(
void *));
654 static constexpr std::uint64_t
increment = 0x9e3779b97f4a7c15ULL;
668 [[nodiscard]]
static constexpr std::uint64_t
mix(std::uint64_t z)
noexcept
670 z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ULL;
671 z = (z ^ (z >> 27)) * 0x94d049bb133111ebULL;
672 return z ^ (z >> 31);
688 constexpr void seed(
const std::uint64_t s)
noexcept {
state = s; }
705 return a.state == b.state;
709 static_assert(std::uniform_random_bit_generator<SplitMix64_Rng>);
711 namespace random_detail
718 return zero() == 0xe220a8397b1dcdafULL
and zero() == 0x6e789e6aa1b965f4ULL
719 and zero() == 0x06c45d188009454fULL
and zero() == 0xf88bb8a8724c81ecULL
720 and other() == 0x599ed017fb08fc85ULL
and other() == 0x2c73f08458540fa5ULL
721 and other() == 0x883ebce5a3f27c77ULL
and other() == 0x3fbef740e9177b3fULL;
725 "SplitMix64_Rng does not give the numbers of SplitMix64");
747 template <std::
unsigned_
integral Word, std::
unsigned_
integral W
ide,
class Next>
750 noexcept(
noexcept(
next()))
753 const auto rejected =
static_cast<Word
>(
static_cast<Word
>(Word{0} - n) % n);
754 while (
static_cast<Word
>(
product) < rejected)
755 product =
static_cast<Wide
>(
static_cast<Wide
>(
next()) * n);
757 return static_cast<Word
>(
product >> std::numeric_limits<Word>::digits);
772 template <std::
unsigned_
integral Word, std::
unsigned_
integral W
ide,
class Next>
775 static_assert(std::numeric_limits<Wide>::digits == 2 * std::numeric_limits<Word>::digits);
777 const auto product =
static_cast<Wide
>(
static_cast<Wide
>(
next()) * n);
778 if (
static_cast<Word
>(
product) >= n) [[likely]]
779 return static_cast<Word
>(
product >> std::numeric_limits<Word>::digits);
781 return bounded_after_a_low_product<Word, Wide>(
next, n,
product);
798 template <std::
unsigned_
integral Word,
class Next>
804 const auto mask =
static_cast<Word
>(
static_cast<Word
>(~Word{0})
805 >> std::countl_zero(
static_cast<Word
>(n - 1)));
806 Word
k =
static_cast<Word
>(
next() & mask);
808 k =
static_cast<Word
>(
next() & mask);
823 (void) std::timespec_get(&now, TIME_UTC);
824 const auto address =
reinterpret_cast<std::uintptr_t
>(&now);
840 std::memory_order_relaxed)
895 const std::uint64_t high = (*g)();
896 return (high << 32) | (*g)();
901 [[gnu::noinline, gnu::cold]] std::uint64_t
wide_uniform_int(
const std::uint64_t n)
noexcept
903 return random_detail::masked<std::uint64_t>([
this] ()
noexcept {
return next64(); }, n);
947 [[nodiscard]]
static constexpr result_type max() noexcept {
return 0xffffffffu; }
993 :
small(o.small),
big(std::move(o.big))
1010 big = std::move(o.big);
1023 std::swap(
small, o.small);
1040 void seed(
const std::uint64_t s)
noexcept
1089 if (
big.
get() ==
nullptr)
1104 [[nodiscard]] gsl_rng * gsl()
1128 unsigned long get() noexcept {
return (*
this)(); }
1149 if (n >
max()) [[unlikely]]
1153 return g->uniform_int_ne(
static_cast<unsigned long>(n));
1155 return random_detail::bounded<std::uint32_t, std::uint64_t>
1156 ([
this] ()
noexcept {
return static_cast<std::uint32_t
>(
small() >> 32); },
1157 static_cast<std::uint32_t
>(n));
1169 if (n == 0) [[unlikely]]
1184 return g->uniform();
1186 return static_cast<double>(
small() >> 11) * 0x1.0p-53;
1200 if (ga ==
nullptr or gb ==
nullptr)
1207 static_assert(std::uniform_random_bit_generator<Random_Source>);
1208 static_assert(std::is_nothrow_default_constructible_v<Random_Source>
1209 and std::is_nothrow_move_constructible_v<Random_Source>
1210 and std::is_nothrow_move_assignable_v<Random_Source>
1211 and not std::is_copy_constructible_v<Random_Source>);
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error()
Throws std::domain_error unconditionally.
A Gsl_Compatible_Rng in dynamic memory, so that what holds it grows only by a pointer,...
std::unique_ptr< Block, Free_Block > block
Gsl_Compatible_Rng_Owner() noexcept=default
An owner without a generator, as after a move; it can take one by a move or a swap.
Gsl_Compatible_Rng_Owner & operator=(const Gsl_Compatible_Rng_Owner &)=delete
Gsl_Compatible_Rng & operator*() const noexcept
The generator.
void swap(Gsl_Compatible_Rng_Owner &o) noexcept
Swaps the generators, with their gsl_rng.
Gsl_Compatible_Rng_Owner(Gsl_Compatible_Rng_Owner &&o) noexcept=default
Takes the generator of o, which keeps none.
Gsl_Compatible_Rng * get() const noexcept
The generator, or nullptr after a move.
Gsl_Compatible_Rng * operator->() const noexcept
The generator.
Gsl_Compatible_Rng_Owner(const Gsl_Compatible_Rng_Owner &)=delete
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).
Gsl_Compatible_Rng() noexcept
A generator with the default seed, as gsl_rng_alloc(gsl_rng_mt19937).
static constexpr result_type max() noexcept
The greatest number of operator()(): 2^32 - 1.
static constexpr int shift_size
std::uint32_t next() noexcept
static constexpr result_type min() noexcept
The least number of operator()().
static void throw_bad_range(const unsigned long n)
std::uint32_t mt[state_size]
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).
static std::uint32_t tempered(std::uint32_t y) noexcept
unsigned long uniform_int_again(const unsigned long n, const unsigned long scale) noexcept
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).
result_type operator()() noexcept
The next number, in [min(), max()], as gsl_rng_get(r).
static std::uint32_t engine_seed(const unsigned long s) noexcept
static constexpr std::uint32_t default_seed
The seed of a new generator, as GSL takes 0.
double uniform() noexcept
A real number in [0, 1), as gsl_rng_uniform(r).
static constexpr int state_size
double exponential(const double mu) noexcept
A real number of the exponential distribution, as gsl_ran_exponential(r, mu).
friend bool operator==(const Gsl_Compatible_Rng &a, const Gsl_Compatible_Rng &b)
Whether two generators give the same numbers from now on.
void init(const std::uint32_t s) noexcept
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()().
static double rounded(const double product) noexcept
std::uint32_t result_type
The type of the numbers of operator()().
Gsl_Compatible_Rng(const unsigned long s) noexcept
A generator with a seed, as gsl_rng_alloc() followed by gsl_rng_set(r, s).
unsigned long uniform_int_ne(const unsigned long n) noexcept
The integer of uniform_int(), without checking n.
The source of random numbers of a randomized structure, which draws from one of two generators.
Random_Source() noexcept
A source that draws from a SplitMix64 with a seed of its own, which changes from a run of the program...
Engine
The generators that a source draws from.
@ Mersenne_Twister
A Gsl_Compatible_Rng, in dynamic memory.
@ SplitMix64
A SplitMix64_Rng, inside the source.
result_type operator()() noexcept
The next number, in [min(), max()].
std::uint64_t next64() noexcept
static constexpr result_type max() noexcept
The greatest number of operator()(): 2^32 - 1.
static std::uint32_t mersenne_word(const std::uint64_t s) noexcept
unsigned long get() noexcept
The next number, as operator()().
Random_Source(const Engine engine, const std::uint64_t s)
A source that draws from a generator with a seed.
static void throw_no_values()
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).
Random_Source(const std::uint64_t s) noexcept
A source that draws from the SplitMix64 of a seed.
std::uint64_t wide_uniform_int(const std::uint64_t n) noexcept
Random_Source(Random_Source &&o) noexcept
Takes the generator of o, with its state, and leaves o with a SplitMix64 with a seed of its own.
Random_Source(const Random_Source &)=delete
A source is not copied: what holds it makes one of its own.
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.
friend bool operator==(const Random_Source &a, const Random_Source &b)
Whether two sources give the same numbers from now on.
static constexpr result_type min() noexcept
The least number of operator()().
std::uint32_t result_type
The type of the numbers of operator()().
Gsl_Compatible_Rng_Owner big
static Gsl_Compatible_Rng_Owner mersenne_twister_of(const std::uint32_t w)
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.
void seed(const Engine engine, const std::uint64_t s)
Draws from now on from a generator with a seed.
Random_Source & operator=(const Random_Source &)=delete
A source is not copied: what holds it makes one of its own.
SplitMix64: a random number generator whose state is a word of 64 bits.
friend constexpr bool operator==(const SplitMix64_Rng &a, const SplitMix64_Rng &b) noexcept
Whether two generators give the same numbers from now on.
static constexpr std::uint64_t increment
What the state advances for each number: 2^64 divided by the golden ratio, as an odd integer.
constexpr SplitMix64_Rng() noexcept
A generator with the default seed.
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.
constexpr SplitMix64_Rng(const std::uint64_t s) noexcept
A generator with a seed.
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 result_type operator()() noexcept
The next number, in [min(), max()].
std::uint64_t result_type
The type of the numbers of operator()().
constexpr void seed(const std::uint64_t s) noexcept
Seeds again.
constexpr Word masked(Next &&next, const Word n) noexcept(noexcept(next()))
An integer in [0, n), each with the same probability, from numbers of the bits of Word,...
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,...
constexpr bool splitmix64_gives_the_numbers_of_its_reference() noexcept
Whether SplitMix64_Rng gives, for the seeds 0 and 1234567, the first numbers of the SplittableRandom ...
constexpr Word bounded(Next &&next, const Word n) noexcept(noexcept(next()))
An integer in [0, n), each with the same probability, from numbers of the bits of Word: the method of...
constexpr Word bounded_after_a_low_product(Next &next, const Word n, Wide product) noexcept(noexcept(next()))
An integer in [0, n), each with the same probability, from numbers of the bits of Word: the method of...
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.
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).
static std::atomic< bool > init
Block(const Block &)=delete
Block(const unsigned long s) noexcept
void(*)(void *) noexcept Free
std::atomic< Free > free_adapter
Block & operator=(const Block &)=delete
std::atomic< void * > adapter
void operator()(Block *b) const noexcept