|
Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
|
The source of random numbers of a randomized structure, which draws from one of two generators. More...
#include <ah-random.H>
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. | |
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.
Definition at line 882 of file ah-random.H.
| 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.
|
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.
|
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.
|
inlineexplicitnoexcept |
A source that draws from the SplitMix64 of a seed.
It does not allocate.
| [in] | s | The seed, as that of SplitMix64_Rng(s). |
Definition at line 959 of file ah-random.H.
|
inline |
A source that draws from a generator with a seed.
| [in] | engine | The generator. |
| [in] | s | The 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. |
| std::bad_alloc | If the engine is the Mersenne Twister and there is not enough memory for it. |
Definition at line 973 of file ah-random.H.
|
delete |
A source is not copied: what holds it makes one of its own.
|
inlinenoexcept |
Takes the generator of o, with its state, and leaves o with a SplitMix64 with a seed of its own.
| [in,out] | o | The source to move from. |
Definition at line 992 of file ah-random.H.
References Aleph::random_detail::seed_of_its_own().
|
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().
|
inlinenoexcept |
The next number, as operator()().
Definition at line 1128 of file ah-random.H.
Referenced by Aleph::Gen_Interval_Tree< NodeType, T, Compare >::insert(), Aleph::Gen_Treap< NodeType, Key, Compare >::insert(), Aleph::Gen_Treap_Rk< NodeType, Key, Compare >::insert(), Aleph::Gen_Interval_Tree< NodeType, T, Compare >::insert_dup(), Aleph::Gen_Treap< NodeType, Key, Compare >::insert_dup(), Aleph::Gen_Treap_Rk< NodeType, Key, Compare >::insert_dup(), Aleph::Gen_Treap< NodeType, Key, Compare >::search_or_insert(), Aleph::Gen_Treap_Rk< NodeType, Key, Compare >::search_or_insert(), TEST(), TEST(), TEST(), and TEST().
|
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().
|
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.
| std::bad_alloc | If 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().
|
inlinestaticprivate |
Definition at line 924 of file ah-random.H.
References Aleph::Gsl_Compatible_Rng::init(), and w.
Referenced by seed().
|
inlinestaticprivatenoexcept |
Definition at line 918 of file ah-random.H.
References Aleph::Gsl_Compatible_Rng::default_seed.
Referenced by seed().
|
inlinestaticconstexprnoexcept |
|
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().
|
inlinenoexcept |
The next number, in [min(), max()].
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.
|
delete |
A source is not copied: what holds it makes one of its own.
|
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.
| [in,out] | o | The source to move from. |
*this. Definition at line 1005 of file ah-random.H.
References big, Aleph::random_detail::seed_of_its_own(), and small.
|
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.
| [in] | engine | The generator. |
| [in] | s | The seed, as in Random_Source(engine, s). |
| std::bad_alloc | If 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.
|
inlinenoexcept |
Draws from now on from the SplitMix64 of a seed.
It frees the Mersenne Twister, if it has one, with the gsl_rng that gsl() gave.
| [in] | s | The seed, as that of SplitMix64_Rng(s). |
Definition at line 1040 of file ah-random.H.
References big, Aleph::SplitMix64_Rng::seed(), and small.
Referenced by Aleph::Gen_Interval_Tree< NodeType, T, Compare >::set_seed(), Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::set_seed(), Aleph::Gen_Treap< NodeType, Key, Compare >::set_seed(), Aleph::Gen_Treap_Rk< NodeType, Key, Compare >::set_seed(), Aleph::DynSkipList< Key, Compare >::set_seed(), Aleph::SkipList< Key, Type >::set_seed(), Aleph::Random_Set< T >::set_seed(), TEST(), TEST(), TEST(), TEST(), and TEST().
|
inlinenoexcept |
Swaps the generators, with their states.
| [in,out] | o | The other source. |
Definition at line 1021 of file ah-random.H.
References big, small, and Aleph::Gsl_Compatible_Rng_Owner::swap().
Referenced by Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::Gen_Rand_Tree(), Aleph::Gen_Interval_Tree< NodeType, T, Compare >::swap(), Aleph::Gen_Rand_Tree< NodeType, Key, Compare >::swap(), Aleph::Gen_Treap< NodeType, Key, Compare >::swap(), Aleph::Gen_Treap_Rk< NodeType, Key, Compare >::swap(), Aleph::Random_Set< T >::swap(), Aleph::SkipList< Key, Type >::swap(), Aleph::DynSkipList< Key, Compare >::swap_but_comparison(), TEST(), and TEST().
|
inlinestaticprivate |
Definition at line 908 of file ah-random.H.
References ah_domain_error.
Referenced by uniform_int().
|
inlinenoexcept |
A real number in [0, 1).
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().
|
inline |
An integer in [0, n), each with the same probability.
| [in] | n | The number of values, which may take the 64 bits with both engines. |
| std::domain_error | If 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().
|
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 |
| [in] | n | The number of values. |
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().
|
inlineprivatenoexcept |
|
friend |
Whether two sources give the same numbers from now on.
| [in] | a | A source. |
| [in] | b | Another one. |
Definition at line 1196 of file ah-random.H.
|
private |
Definition at line 885 of file ah-random.H.
Referenced by engine(), mersenne_twister(), next64(), operator()(), operator=(), seed(), seed(), swap(), uniform(), and uniform_int_ne().
|
private |
Definition at line 884 of file ah-random.H.
Referenced by mersenne_twister(), next64(), operator()(), operator=(), seed(), swap(), uniform(), and uniform_int_ne().