Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::Random_Source Class Reference

The source of random numbers of a randomized structure, which draws from one of two generators. More...

#include <ah-random.H>

Collaboration diagram for Aleph::Random_Source:
[legend]

Public Types

enum class  Engine { SplitMix64 , Mersenne_Twister }
 The generators that a source draws from. More...
 
using result_type = std::uint32_t
 The type of the numbers of operator()().
 

Public Member Functions

 Random_Source () noexcept
 A source that draws from a SplitMix64 with a seed of its own, which changes from a run of the program to another.
 
 Random_Source (const std::uint64_t s) noexcept
 A source that draws from the SplitMix64 of a seed.
 
 Random_Source (const Engine engine, const std::uint64_t s)
 A source that draws from a generator with a seed.
 
 Random_Source (const Random_Source &)=delete
 A source is not copied: what holds it makes one of its own.
 
Random_Source & operator= (const Random_Source &)=delete
 A source is not copied: what holds it makes one of its own.
 
 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 & operator= (Random_Source &&o) noexcept
 Frees the Mersenne Twister, if it has one, takes the generator of o, with its state, and leaves o with a SplitMix64 with a seed of its own.
 
void swap (Random_Source &o) noexcept
 Swaps the generators, with their states.
 
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 seed (const Engine engine, const std::uint64_t s)
 Draws from now on from a generator with a seed.
 
Gsl_Compatible_Rng & mersenne_twister ()
 The Mersenne Twister of the source, which is its engine from now on.
 
result_type operator() () noexcept
 The next number, in [min(), max()].
 
unsigned long get () noexcept
 The next number, as 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.
 
std::uint64_t uniform_int (const std::uint64_t n)
 An integer in [0, n), each with the same probability.
 
double uniform () noexcept
 A real number in [0, 1).
 

Static Public Member Functions

static constexpr result_type min () noexcept
 The least number of operator()().
 
static constexpr result_type max () noexcept
 The greatest number of operator()(): 2^32 - 1.
 

Private Member Functions

std::uint64_t next64 () noexcept
 
std::uint64_t wide_uniform_int (const std::uint64_t n) noexcept
 

Static Private Member Functions

static void throw_no_values ()
 
static std::uint32_t mersenne_word (const std::uint64_t s) noexcept
 
static Gsl_Compatible_Rng_Owner mersenne_twister_of (const std::uint32_t w)
 

Private Attributes

SplitMix64_Rng small
 
Gsl_Compatible_Rng_Owner big
 

Friends

bool operator== (const Random_Source &a, const Random_Source &b)
 Whether two sources give the same numbers from now on.
 

Detailed Description

The source of random numbers of a randomized structure, which draws from one of two generators.

Engine Generator Its state Who asks for it
Engine::SplitMix64 SplitMix64_Rng A word, inside the source Random_Source(), Random_Source(s), seed(s)
Engine::Mersenne_Twister Gsl_Compatible_Rng About 2500 bytes, in dynamic memory Random_Source(engine, s), seed(engine, s), mersenne_twister()

A source built by default draws from a SplitMix64 with a seed of its own: one that no other source of the program is given, and that changes from a run of the program to another. Such a source is built, moved and swapped without allocating and without throwing; what a structure that holds one does in those operations, it says itself. A source built with a seed alone draws from the SplitMix64 of that seed: the same numbers on every run and on every platform, without allocating either.

A source that is asked for the Mersenne Twister keeps it in memory of malloc(), which the constructor or the member that asks for it takes, and which may throw std::bad_alloc. Its numbers, and its draws of uniform() and of uniform_int() up to 2^32 - 1 values, are those of GSL for the seed, bit by bit: see Gsl_Compatible_Rng.

The numbers of operator()() and get() have 32 bits with both engines: those of the Mersenne Twister, and the high half of those of SplitMix64. It satisfies std::uniform_random_bit_generator.

It is not copied: a structure that is copied makes a source of its own. A source that is moved from goes back to a SplitMix64 with a seed of its own, and can be drawn from.

Note
A draw changes the state of the generator, and mersenne_twister() may make one: as any generator, a source must not be used from two threads at once.

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

Member Typedef Documentation

◆ result_type

using Aleph::Random_Source::result_type = std::uint32_t

The type of the numbers of operator()().

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

Member Enumeration Documentation

◆ Engine

enum class Aleph::Random_Source::Engine
strong

The generators that a source draws from.

Enumerator
SplitMix64 

A SplitMix64_Rng, inside the source.

Mersenne_Twister 

A Gsl_Compatible_Rng, in dynamic memory.

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

Constructor & Destructor Documentation

◆ Random_Source() [1/5]

Aleph::Random_Source::Random_Source ( )
inlinenoexcept

A source that draws from a SplitMix64 with a seed of its own, which changes from a run of the program to another.

It does not allocate.

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

◆ Random_Source() [2/5]

Aleph::Random_Source::Random_Source ( const std::uint64_t  s)
inlineexplicitnoexcept

A source that draws from the SplitMix64 of a seed.

It does not allocate.

Parameters
[in]sThe seed, as that of SplitMix64_Rng(s).

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

◆ Random_Source() [3/5]

Aleph::Random_Source::Random_Source ( const Engine  engine,
const std::uint64_t  s 
)
inline

A source that draws from a generator with a seed.

Parameters
[in]engineThe generator.
[in]sThe seed: of SplitMix64_Rng(s), or of the Mersenne Twister as GSL takes a seed where unsigned long has 64 bits: 0 as 4357, and else its low 32 bits, which may be 0. So, on every platform, a seed below 2^32 is that of Gsl_Compatible_Rng(s), and one above it gives the same numbers.
Exceptions
std::bad_allocIf the engine is the Mersenne Twister and there is not enough memory for it.

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

◆ Random_Source() [4/5]

Aleph::Random_Source::Random_Source ( const Random_Source &  )
delete

A source is not copied: what holds it makes one of its own.

◆ Random_Source() [5/5]

Aleph::Random_Source::Random_Source ( Random_Source &&  o)
inlinenoexcept

Takes the generator of o, with its state, and leaves o with a SplitMix64 with a seed of its own.

Parameters
[in,out]oThe source to move from.

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

References Aleph::random_detail::seed_of_its_own().

Member Function Documentation

◆ engine()

Engine Aleph::Random_Source::engine ( ) const
inlinenoexcept

The generator that the source draws from.

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

References big, Aleph::Gsl_Compatible_Rng_Owner::get(), Mersenne_Twister, and SplitMix64.

Referenced by seed(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().

◆ get()

◆ max()

static constexpr result_type Aleph::Random_Source::max ( )
inlinestaticconstexprnoexcept

The greatest number of operator()(): 2^32 - 1.

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

Referenced by TEST(), and uniform_int_ne().

◆ mersenne_twister()

Gsl_Compatible_Rng & Aleph::Random_Source::mersenne_twister ( )
inline

The Mersenne Twister of the source, which is its engine from now on.

A source that draws from SplitMix64 makes it, seeded with the high 32 bits of the next number of its SplitMix64, and draws from it from then on. What is done to it through the reference, as seeding it or drawing from it, is done to the generator of the source.

Returns
The Mersenne Twister. The reference follows it: when the source is moved or swapped, it is that of the source to which the generator went. It is no longer valid once the source that has the generator is destroyed, is given SplitMix64 with seed(), or is the target of a move assignment, which frees it; a move assignment of the source to itself frees nothing.
Exceptions
std::bad_allocIf the source has none and there is not enough memory for it; then the source is unchanged.

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

References big, Aleph::Gsl_Compatible_Rng_Owner::get(), and small.

Referenced by Aleph::DynSkipList< Key, Compare >::rng(), Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::rng(), Aleph::SkipList< Key, Type >::rng(), Aleph::Gen_Treap< NodeType, Key, Compare >::rng(), Aleph::Gen_Treap_Rk< NodeType, Key, Compare >::rng(), Aleph::Gen_Interval_Tree< NodeType, T, Compare >::rng(), Aleph::Random_Set< T >::rng(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TYPED_TEST().

◆ mersenne_twister_of()

static Gsl_Compatible_Rng_Owner Aleph::Random_Source::mersenne_twister_of ( const std::uint32_t  w)
inlinestaticprivate

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

References Aleph::Gsl_Compatible_Rng::init(), and w.

Referenced by seed().

◆ mersenne_word()

static std::uint32_t Aleph::Random_Source::mersenne_word ( const std::uint64_t  s)
inlinestaticprivatenoexcept

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

References Aleph::Gsl_Compatible_Rng::default_seed.

Referenced by seed().

◆ min()

static constexpr result_type Aleph::Random_Source::min ( )
inlinestaticconstexprnoexcept

The least number of operator()().

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

Referenced by TEST().

◆ next64()

std::uint64_t Aleph::Random_Source::next64 ( )
inlineprivatenoexcept

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

References big, Aleph::Gsl_Compatible_Rng_Owner::get(), and small.

Referenced by wide_uniform_int().

◆ operator()()

result_type Aleph::Random_Source::operator() ( )
inlinenoexcept

The next number, in [min(), max()].

Returns
That of the Mersenne Twister, as gsl_rng_get(), or the high 32 bits of that of SplitMix64.

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

References big, Aleph::Gsl_Compatible_Rng_Owner::get(), and small.

◆ operator=() [1/2]

Random_Source & Aleph::Random_Source::operator= ( const Random_Source &  )
delete

A source is not copied: what holds it makes one of its own.

◆ operator=() [2/2]

Random_Source & Aleph::Random_Source::operator= ( Random_Source &&  o)
inlinenoexcept

Frees the Mersenne Twister, if it has one, takes the generator of o, with its state, and leaves o with a SplitMix64 with a seed of its own.

Parameters
[in,out]oThe source to move from.
Returns
*this.

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

References big, Aleph::random_detail::seed_of_its_own(), and small.

◆ seed() [1/2]

void Aleph::Random_Source::seed ( const Engine  engine,
const std::uint64_t  s 
)
inline

Draws from now on from a generator with a seed.

A Mersenne Twister that the source already has is seeded again where it is: a reference to it, and the gsl_rng of gsl(), are still valid.

Parameters
[in]engineThe generator.
[in]sThe seed, as in Random_Source(engine, s).
Exceptions
std::bad_allocIf the engine is the Mersenne Twister, the source has none and there is not enough memory for it; then the source is unchanged.

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

References big, engine(), Aleph::Gsl_Compatible_Rng_Owner::get(), mersenne_twister_of(), mersenne_word(), seed, and SplitMix64.

◆ seed() [2/2]

void Aleph::Random_Source::seed ( const std::uint64_t  s)
inlinenoexcept

◆ swap()

◆ throw_no_values()

static void Aleph::Random_Source::throw_no_values ( )
inlinestaticprivate

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

References ah_domain_error.

Referenced by uniform_int().

◆ uniform()

double Aleph::Random_Source::uniform ( )
inlinenoexcept

A real number in [0, 1).

Returns
With the Mersenne Twister, its next number divided by 2^32, as gsl_rng_uniform(); with SplitMix64, the high 53 bits of its next number divided by 2^53.

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

References big, Aleph::Gsl_Compatible_Rng_Owner::get(), and small.

Referenced by Aleph::SkipList< Key, Type >::generateRandomLevel(), Aleph::DynSkipList< Key, Compare >::random_level(), TEST(), TEST(), TEST(), TEST(), and TEST().

◆ uniform_int()

std::uint64_t Aleph::Random_Source::uniform_int ( const std::uint64_t  n)
inline

An integer in [0, n), each with the same probability.

Parameters
[in]nThe number of values, which may take the 64 bits with both engines.
Returns
The integer of uniform_int_ne().
Exceptions
std::domain_errorIf n is 0.

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

References throw_no_values(), and uniform_int_ne().

Referenced by TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().

◆ uniform_int_ne()

std::uint64_t Aleph::Random_Source::uniform_int_ne ( const std::uint64_t  n)
inlinenoexcept

An integer in [0, n), each with the same probability, without checking n.

For a caller that knows that n is not 0, as a structure that draws a position among its items.

Engine n The integer
Mersenne Twister Up to 2^32 - 1 That of gsl_rng_uniform_int(): Gsl_Compatible_Rng::uniform_int()
SplitMix64 Up to 2^32 - 1 The method of Lemire on a number of 32 bits
Both Above 2^32 - 1 A number of 64 bits under a mask, until one is below n; with the Mersenne Twister, of two of its numbers
Parameters
[in]nThe number of values.
Returns
The integer.
Precondition
n is not 0: with 0, what it returns is not specified, and with the Mersenne Twister it divides by zero.

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

References big, Aleph::Gsl_Compatible_Rng_Owner::get(), max(), small, and wide_uniform_int().

Referenced by Aleph::Random_Set< T >::append(), Aleph::Random_Set< T >::append(), Aleph::Random_Set< T >::get(), Aleph::Random_Set< T >::pick(), Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::random_insert(), Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::random_insert_dup(), Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::random_join(), Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::random_join(), Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::random_join_exclusive(), Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::random_search_or_insert(), TEST(), TEST(), TEST(), TEST(), TEST(), and uniform_int().

◆ wide_uniform_int()

std::uint64_t Aleph::Random_Source::wide_uniform_int ( const std::uint64_t  n)
inlineprivatenoexcept

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

References next64().

Referenced by uniform_int_ne().

Friends And Related Symbol Documentation

◆ operator==

bool operator== ( const Random_Source &  a,
const Random_Source &  b 
)
friend

Whether two sources give the same numbers from now on.

Parameters
[in]aA source.
[in]bAnother one.
Returns
Whether they draw from the same engine, and the states of their generators are equal.

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

Member Data Documentation

◆ big

Gsl_Compatible_Rng_Owner Aleph::Random_Source::big
private

◆ small

SplitMix64_Rng Aleph::Random_Source::small
private

The documentation for this class was generated from the following file: