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

A random number generator with the sequences of GSL's gsl_rng_mt19937. More...

#include <ah-random.H>

Public Types

using result_type = std::uint32_t
 The type of the numbers of operator()().
 

Public Member Functions

 Gsl_Compatible_Rng () noexcept
 A generator with the default seed, as gsl_rng_alloc(gsl_rng_mt19937).
 
 Gsl_Compatible_Rng (const unsigned long s) noexcept
 A generator with a seed, as gsl_rng_alloc() followed by gsl_rng_set(r, s).
 
void seed (const unsigned long s) noexcept
 Seeds again, as gsl_rng_set(r, s).
 
result_type operator() () noexcept
 The next number, in [min(), max()], as gsl_rng_get(r).
 
unsigned long get () noexcept
 The next number, as operator()().
 
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).
 
unsigned long uniform_int_ne (const unsigned long n) noexcept
 The integer of uniform_int(), without checking n.
 
double uniform () noexcept
 A real number in [0, 1), as gsl_rng_uniform(r).
 
double uniform_pos () noexcept
 A real number in (0, 1), as gsl_rng_uniform_pos(r).
 
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).
 
double gaussian (const double sigma) noexcept
 A real number of the normal distribution of mean 0, as gsl_ran_gaussian(r, sigma).
 
double exponential (const double mu) noexcept
 A real number of the exponential distribution, as gsl_ran_exponential(r, mu).
 

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.
 

Static Public Attributes

static constexpr std::uint32_t default_seed = 4357
 The seed of a new generator, as GSL takes 0.
 

Private Member Functions

void init (const std::uint32_t s) noexcept
 
void generate () noexcept
 
std::uint32_t next () noexcept
 
unsigned long uniform_int_again (const unsigned long n, const unsigned long scale) noexcept
 

Static Private Member Functions

static std::uint32_t tempered (std::uint32_t y) noexcept
 
static std::uint32_t engine_seed (const unsigned long s) noexcept
 
static void throw_bad_range (const unsigned long n)
 
static double rounded (const double product) noexcept
 

Private Attributes

std::uint32_t mt [state_size]
 
int mti
 

Static Private Attributes

static constexpr int state_size = 624
 
static constexpr int shift_size = 397
 

Friends

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

Detailed Description

A random number generator with the sequences of GSL's gsl_rng_mt19937.

For each seed, the numbers and the draws are those of the functions of GSL named on each member, bit by bit, but for what the documentation of the file says of flat() and gaussian():

Member GSL
Gsl_Compatible_Rng(), seed(s) gsl_rng_alloc(gsl_rng_mt19937), gsl_rng_set(r, s)
operator()(), get() gsl_rng_get(r)
uniform_int(n) gsl_rng_uniform_int(r, n)
uniform() gsl_rng_uniform(r)
uniform_pos() gsl_rng_uniform_pos(r)
flat(a, b) gsl_ran_flat(r, a, b)
gaussian(sigma) gsl_ran_gaussian(r, sigma)
exponential(mu) gsl_ran_exponential(r, mu)

It satisfies std::uniform_random_bit_generator, so that it can feed the distributions and algorithms of the standard library too.

The numbers come from inline code, without the call through a pointer to a function that GSL makes for each one.

Note
It holds the state of the Mersenne Twister, 624 words of 32 bits and the position of the next one: about 2500 bytes. A structure that holds one may keep it in dynamic memory.

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

Member Typedef Documentation

◆ result_type

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

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

Constructor & Destructor Documentation

◆ Gsl_Compatible_Rng() [1/2]

Aleph::Gsl_Compatible_Rng::Gsl_Compatible_Rng ( )
inlinenoexcept

A generator with the default seed, as gsl_rng_alloc(gsl_rng_mt19937).

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

References default_seed, and Aleph::init.

◆ Gsl_Compatible_Rng() [2/2]

Aleph::Gsl_Compatible_Rng::Gsl_Compatible_Rng ( const unsigned long  s)
inlineexplicitnoexcept

A generator with a seed, as gsl_rng_alloc() followed by gsl_rng_set(r, s).

Parameters
[in]sThe seed: 0 is taken as 4357, and else its low 32 bits count.

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

References engine_seed(), and Aleph::init.

Member Function Documentation

◆ engine_seed()

static std::uint32_t Aleph::Gsl_Compatible_Rng::engine_seed ( const unsigned long  s)
inlinestaticprivatenoexcept

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

References default_seed.

Referenced by Gsl_Compatible_Rng(), and seed().

◆ exponential()

double Aleph::Gsl_Compatible_Rng::exponential ( const double  mu)
inlinenoexcept

A real number of the exponential distribution, as gsl_ran_exponential(r, mu).

Parameters
[in]muThe mean.
Returns
-mu log(1 - u), for u of uniform().

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

References uniform().

Referenced by Random_Network_Flow< Net >::connect_ranks(), TEST(), and TEST().

◆ flat()

double Aleph::Gsl_Compatible_Rng::flat ( const double  a,
const double  b 
)
inlinenoexcept

A real number of the uniform distribution on [a, b), as gsl_ran_flat(r, a, b).

Parameters
[in]aThe least value.
[in]bThe bound of the values.
Returns
a (1 - u) + b u, for u of uniform(), with each product rounded.

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

References rounded(), and uniform().

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

◆ gaussian()

double Aleph::Gsl_Compatible_Rng::gaussian ( const double  sigma)
inlinenoexcept

A real number of the normal distribution of mean 0, as gsl_ran_gaussian(r, sigma).

The polar method of Box and Muller: a point of the square (-1, 1)^2, of uniform_pos(), until it falls inside the unit circle and is not its center; the squares of its coordinates are rounded before their sum.

Parameters
[in]sigmaThe standard deviation.
Returns
The number.

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

References rounded(), uniform_pos(), and y.

Referenced by Random_Network_Flow< Net >::connect_ranks(), Random_Network_Flow< Net >::create_ranks(), and TEST().

◆ generate()

void Aleph::Gsl_Compatible_Rng::generate ( )
inlineprivatenoexcept

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

References k, mt, mti, shift_size, state_size, and y.

Referenced by next().

◆ get()

◆ init()

void Aleph::Gsl_Compatible_Rng::init ( const std::uint32_t  s)
inlineprivatenoexcept

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

References mt, mti, and state_size.

Referenced by Aleph::Random_Source::mersenne_twister_of().

◆ max()

◆ min()

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

The least number of operator()().

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

Referenced by Aleph::Reservoir_Sampler< T >::Reservoir_Sampler(), main(), and TEST().

◆ next()

std::uint32_t Aleph::Gsl_Compatible_Rng::next ( )
inlineprivatenoexcept

◆ operator()()

result_type Aleph::Gsl_Compatible_Rng::operator() ( )
inlinenoexcept

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

Returns
The number.

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

References next().

◆ rounded()

static double Aleph::Gsl_Compatible_Rng::rounded ( const double  product)
inlinestaticprivatenoexcept

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

References Aleph::product().

Referenced by flat(), and gaussian().

◆ seed()

void Aleph::Gsl_Compatible_Rng::seed ( const unsigned long  s)
inlinenoexcept

Seeds again, as gsl_rng_set(r, s).

Parameters
[in]sThe seed: 0 is taken as 4357, and else its low 32 bits count.

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

References engine_seed(), and Aleph::init.

Referenced by Aleph::Reservoir_Sampler< T >::clear(), init_random(), main(), Aleph::Karger_Min_Cut< GT, SA >::reseed(), TEST(), TEST(), TEST(), TYPED_TEST(), and TYPED_TEST().

◆ tempered()

static std::uint32_t Aleph::Gsl_Compatible_Rng::tempered ( std::uint32_t  y)
inlinestaticprivatenoexcept

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

References y.

Referenced by next(), and uniform_int_ne().

◆ throw_bad_range()

static void Aleph::Gsl_Compatible_Rng::throw_bad_range ( const unsigned long  n)
inlinestaticprivate

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

References ah_domain_error, and max().

Referenced by uniform_int().

◆ uniform()

double Aleph::Gsl_Compatible_Rng::uniform ( )
inlinenoexcept

A real number in [0, 1), as gsl_rng_uniform(r).

Returns
The next number divided by 2^32.

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

References next().

Referenced by Aleph::Random_Digraph< GT, Init_Node, Init_Arc >::create_p(), Aleph::Random_Graph< GT, Init_Node, Init_Arc >::create_p(), exponential(), flat(), TEST(), TEST(), TEST(), and uniform_pos().

◆ uniform_int()

unsigned long Aleph::Gsl_Compatible_Rng::uniform_int ( const unsigned long  n)
inline

An integer in [0, n), each with the same probability, as gsl_rng_uniform_int(r, n).

It divides a number by (2^32 - 1) / n, and takes another one while the quotient is n or more.

Parameters
[in]nThe number of values, in [1, max()].
Returns
The integer.
Exceptions
std::domain_errorIf n is 0 or greater than max(). GSL calls its error handler, which ends the program by default.

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

References max(), throw_bad_range(), and uniform_int_ne().

Referenced by Aleph::Random_Digraph< GT, Init_Node, Init_Arc >::balance_digraph_node(), Aleph::Random_Digraph< GT, Init_Node, Init_Arc >::balance_digraph_nodes_degree(), Aleph::Random_Graph< GT, Init_Node, Init_Arc >::balance_graph_nodes_degree(), Random_Network_Flow< Net >::connect_ranks(), Aleph::Karger_Min_Cut< GT, SA >::draw_arc(), get_rand(), Aleph::Random_Digraph< GT, Init_Node, Init_Arc >::join(), Aleph::Random_Graph< GT, Init_Node, Init_Arc >::join_random_odd_nodes(), Aleph::Random_Graph< GT, Init_Node, Init_Arc >::join_through_a_node(), main(), Aleph::Random_Digraph< GT, Init_Node, Init_Arc >::make_eulerian(), Aleph::Init_P< GT >::operator()(), Aleph::Init_Arc< GT >::operator()(), TenVectors::rand_vec(), RandTree< T >::random(), Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >::select_random_node(), Aleph::Random_Graph_Base< GT, Init_Node, Init_Arc >::select_random_node(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), test(), TEST_F(), test_hash_table(), and Aleph::Reservoir_Sampler< T >::update().

◆ uniform_int_again()

unsigned long Aleph::Gsl_Compatible_Rng::uniform_int_again ( const unsigned long  n,
const unsigned long  scale 
)
inlineprivatenoexcept

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

References k, and next().

Referenced by uniform_int_ne().

◆ uniform_int_ne()

unsigned long Aleph::Gsl_Compatible_Rng::uniform_int_ne ( const unsigned long  n)
inlinenoexcept

The integer of uniform_int(), without checking n.

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

Parameters
[in]nThe number of values.
Returns
The integer, in [0, n).
Precondition
n is in [1, max()]: with 0 it divides by zero.

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

References k, max(), mt, mti, state_size, tempered(), and uniform_int_again().

Referenced by TEST(), and uniform_int().

◆ uniform_pos()

double Aleph::Gsl_Compatible_Rng::uniform_pos ( )
inlinenoexcept

A real number in (0, 1), as gsl_rng_uniform_pos(r).

Returns
The first number of uniform() that is not 0.

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

References uniform().

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

Friends And Related Symbol Documentation

◆ operator==

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

Whether two generators give the same numbers from now on.

Parameters
[in]aA generator.
[in]bAnother one.
Returns
Whether their states are equal.

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

◆ Random_Source

friend class Random_Source
friend

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

Member Data Documentation

◆ default_seed

constexpr std::uint32_t Aleph::Gsl_Compatible_Rng::default_seed = 4357
staticconstexpr

The seed of a new generator, as GSL takes 0.

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

Referenced by Gsl_Compatible_Rng(), engine_seed(), and Aleph::Random_Source::mersenne_word().

◆ mt

std::uint32_t Aleph::Gsl_Compatible_Rng::mt[state_size]
private

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

Referenced by generate(), init(), next(), and uniform_int_ne().

◆ mti

int Aleph::Gsl_Compatible_Rng::mti
private

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

Referenced by generate(), init(), next(), and uniform_int_ne().

◆ shift_size

constexpr int Aleph::Gsl_Compatible_Rng::shift_size = 397
staticconstexprprivate

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

Referenced by generate().

◆ state_size

constexpr int Aleph::Gsl_Compatible_Rng::state_size = 624
staticconstexprprivate

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

Referenced by generate(), init(), next(), and uniform_int_ne().


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