Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ah-random.H File Reference

The random number generators of Aleph: the one that gives the numbers of the gsl_rng_mt19937 of GSL, without GSL, SplitMix64, and the source of random numbers of the randomized structures, which draws from one or the other. More...

#include <algorithm>
#include <atomic>
#include <bit>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <ctime>
#include <limits>
#include <memory>
#include <new>
#include <random>
#include <type_traits>
#include <utility>
#include <ah-errors.H>
Include dependency graph for ah-random.H:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  Aleph::Gsl_Compatible_Rng
 A random number generator with the sequences of GSL's gsl_rng_mt19937. More...
 
class  Aleph::Gsl_Compatible_Rng_Owner
 A Gsl_Compatible_Rng in dynamic memory, so that what holds it grows only by a pointer, as with the gsl_rng of before. More...
 
struct  Aleph::Gsl_Compatible_Rng_Owner::Block
 
struct  Aleph::Gsl_Compatible_Rng_Owner::Free_Block
 
class  Aleph::SplitMix64_Rng
 SplitMix64: a random number generator whose state is a word of 64 bits. More...
 
class  Aleph::Random_Source
 The source of random numbers of a randomized structure, which draws from one of two generators. More...
 

Namespaces

namespace  Aleph
 Main namespace for Aleph-w library functions.
 
namespace  Aleph::random_detail
 

Macros

#define ALEPH_WITH_GSL   0
 Whether the deprecated accessors to a gsl_rng of the randomized structures exist.
 

Functions

constexpr bool Aleph::random_detail::splitmix64_gives_the_numbers_of_its_reference () noexcept
 Whether SplitMix64_Rng gives, for the seeds 0 and 1234567, the first numbers of the SplittableRandom of Java and of splitmix64.c.
 
template<std::unsigned_integral Word, std::unsigned_integral Wide, class Next >
constexpr Word Aleph::random_detail::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 Lemire.
 
template<std::unsigned_integral Word, std::unsigned_integral Wide, class Next >
constexpr Word Aleph::random_detail::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 Lemire, which bounded_after_a_low_product() describes.
 
template<std::unsigned_integral Word, class Next >
constexpr Word Aleph::random_detail::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, without a product of twice the bits: a number under the least mask of ones that covers n - 1, until one is below n.
 
std::uint64_t Aleph::random_detail::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; the processor time that the program has taken; and the address of a variable of the stack, which the system may place at random.
 
std::uint64_t Aleph::random_detail::seed_of_its_own () noexcept
 The seed of a Random_Source that is given none: the next number of a SplitMix64 of the process, which is seeded with entropy_of_the_run() when it gives the first one.
 

Detailed Description

The random number generators of Aleph: the one that gives the numbers of the gsl_rng_mt19937 of GSL, without GSL, SplitMix64, and the source of random numbers of the randomized structures, which draws from one or the other.

Random_Source is what a randomized structure holds. By default it draws from a SplitMix64_Rng, whose state is a word inside the structure: it is built, moved and swapped without allocating. When it is asked for the Mersenne Twister, it draws from a Gsl_Compatible_Rng in dynamic memory.

Gsl_Compatible_Rng is the Mersenne Twister MT19937, the algorithm of gsl_rng_mt19937 and of std::mt19937, whose output the standard fixes, seeded as GSL seeds it. Its draws follow the algorithms of GSL. So, for each seed, its integers and the reals of uniform() and uniform_pos() are those of GSL, bit by bit, on every platform, and a structure or a test that took its numbers from GSL takes the same ones from it.

It keeps the state of the Mersenne Twister itself, instead of in a std::mt19937, so that the step of each number is inline code, which a compiler inlines where a number is drawn: GCC does not inline the operator of std::mt19937, which is not declared inline, in a unit where it is called from several places.

flat(), gaussian() and exponential() take the logarithm and the square root of the C library, as GSL does; flat() and gaussian() round each product, so that a fused multiply-add of the compiler cannot change their draws, which do not depend on the options with which a program is built. On a platform, they are those of GSL where GSL rounds them too, as in the builds of the distributions for x86-64; where GSL is built with fused multiply-adds, as for arm64, a draw of flat() or gaussian() may differ from that of GSL in its last bits. So may a draw of gaussian() or exponential() where the program and GSL take the logarithm of different C libraries, as a program of MSVC, which takes that of the UCRT, and the GSL of MSYS2.

Definition in file ah-random.H.

Macro Definition Documentation

◆ ALEPH_WITH_GSL

#define ALEPH_WITH_GSL   0

Whether the deprecated accessors to a gsl_rng of the randomized structures exist.

It is 1 if the headers of GSL can be included, unless it is defined before, as 0 to leave the accessors out. While it is 1, a structure whose gsl_rng_object() is called gives a gsl_rng whose numbers come from its Gsl_Compatible_Rng. Aleph built without GSL (the CMake option ALEPH_WITH_GSL set to OFF, its default from 8.0.0) defines it as 0 for itself and for the programs that link Aleph::Aleph, so that they neither include nor need GSL.

Definition at line 106 of file ah-random.H.