|
ADS
Algorithms and Data Structures
|
The engineered random numbers, beyond the book. More...
Files | |
| file | ADS/numeric/RandomGenerators.h |
| The engineered random numbers, beyond the book: generators of 64 bits that cost a few instructions and pass the statistical batteries (BigCrush of TestU01, PractRand), and the uses of a word that need no division. | |
Concepts | |
| concept | ADS::FullWordGenerator |
| a std::uniform_random_bit_generator of 64 full bits, from 0 to \( 2^{64} - 1 \) | |
Classes | |
| class | ADS::Xoshiro256T< StarStar > |
| xoshiro256++ (or xoshiro256** if StarStar), a std::uniform_random_bit_generator of 64 bits More... | |
| class | ADS::WyRand |
| < xoshiro256** More... | |
| class | ADS::PhiloxT< W > |
| Philox4xW-10, the counter-based std::uniform_random_bit_generator of words of \( W \) bits (32 or 64) of Salmon, Moraes, Dror and Shaw, with the sequence of std::philox4x32 and std::philox4x64 of C++26. More... | |
| class | ADS::AliasTable |
| < Philox4x64-10 More... | |
Macros | |
| #define | ADS_RNG_INLINE ADS_ALWAYS_INLINE |
| a function inlined where it is used, so that the state of the generator stays in the registers of the caller's loop | |
Functions | |
| template<FullWordGenerator G> | |
| std::uint64_t | ADS::uniformBelow (G &&g, std::uint64_t s) |
| an integer uniform in \( [ 0 , s ) \), by the method of Lemire | |
| template<FullWordGenerator G> | |
| double | ADS::uniformReal (G &&g) |
| a double uniform in \( [ 0 , 1 ) \), the top 53 bits of a word times \( 2^{-53} \) | |
| template<FullWordGenerator G> | |
| double | ADS::normalZiggurat (G &&g) |
| a normal deviate of mean 0 and variance 1, by the ziggurat of Marsaglia and Tsang with 256 layers | |
| template<FullWordGenerator G> | |
| double | ADS::exponentialZiggurat (G &&g) |
| an exponential deviate of mean 1, by the ziggurat of Marsaglia and Tsang with 256 layers | |
| template<std::random_access_iterator I, FullWordGenerator G> | |
| void | ADS::shuffleFast (I first, I last, G &&g) |
| the items of \( [ \mathit{first} , \mathit{last} ) \) shuffled by \( g \), every permutation equally likely, the indices drawn in batches | |
The engineered random numbers, beyond the book.
The definitions, the invariants and the costs of the area are in Theory of numerical algorithms.
| #define ADS_RNG_INLINE ADS_ALWAYS_INLINE |
a function inlined where it is used, so that the state of the generator stays in the registers of the caller's loop
| double ADS::exponentialZiggurat | ( | G && | g | ) |
an exponential deviate of mean 1, by the ziggurat of Marsaglia and Tsang with 256 layers
In pseudocode (Marsaglia and Tsang, beyond the book), the tail drawn again from \( r = x_1 \), since the density has no memory:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{exponentialZiggurat}( g ) \cr 2 & \quad b \gets 0 \cr 3 & \quad \textbf{loop} \cr 4 & \qquad w \gets g();\ i \gets w \bmod 256;\ u \gets \text{the top 53 } \text{bits of } w \text{ in } [ 0 , 1 );\ x \gets u x_i \cr 5 & \qquad \textbf{if}\ u < x_{i+1} / x_i\ \textbf{then return}\ b + x \cr 6 & \qquad \textbf{if}\ i = 0\ \textbf{then}\ b \gets b + r \cr 7 & \qquad \textbf{else if}\ y_{i+1} + ( y_i - y_{i+1} ) U < e^{-x} \ \textbf{then return}\ b + x \end{array}} \]
| double ADS::normalZiggurat | ( | G && | g | ) |
a normal deviate of mean 0 and variance 1, by the ziggurat of Marsaglia and Tsang with 256 layers
In pseudocode (Marsaglia and Tsang, beyond the book), \( x_i \) the right ends of the layers, \( y_i \) the density there, and \( r = x_1 \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{normalZiggurat}( g ) \cr 2 & \quad \textbf{loop} \cr 3 & \qquad w \gets g();\ i \gets w \bmod 256;\ u \gets \text{the top 53 } \text{bits of } w \text{ in } [ -1 , 1 ) \cr 4 & \qquad x \gets u x_i \cr 5 & \qquad \textbf{if}\ |u| < x_{i+1} / x_i\ \textbf{then return}\ x \cr 6 & \qquad \textbf{if}\ i = 0\ \textbf{then} \cr 7 & \qquad\quad \textbf{repeat}\ a \gets -\ln( U_1 ) / r;\ b \gets -\ln( U_2 )\ \textbf{until}\ 2 b \ge a^2 \cr 8 & \qquad\quad \textbf{return}\ \pm ( r + a ) \text{, the sign of } u \cr 9 & \qquad \textbf{if}\ y_{i+1} + ( y_i - y_{i+1} ) U < e^{-x^2/2} \ \textbf{then return}\ x \end{array}} \]
| void ADS::shuffleFast | ( | I | first, |
| I | last, | ||
| G && | g ) |
the items of \( [ \mathit{first} , \mathit{last} ) \) shuffled by \( g \), every permutation equally likely, the indices drawn in batches
In pseudocode (Fisher and Yates, with the batches of Brackett-Rozinsky and Lemire, beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{shuffleFast}( A , g )\quad \triangleright\ n = |A| \cr 2 & \quad \textbf{while}\ n > 1\ \textbf{do} \cr 3 & \qquad k \gets 1 \text{ if } n > 2^{32} \text{, } 2 \text{ if } n > 2^{10} \text{, } 6 \text{ if } n > 6 \text{, else } 1 \cr 4 & \qquad p \gets n ( n - 1 ) \cdots ( n - k + 1 ) \cr 5 & \qquad \textbf{repeat}\ l \gets g() \cr 6 & \qquad\quad \textbf{for}\ j \gets 0\ \textbf{to}\ k - 1\ \textbf{do} \ ( \mathit{idx}_j , l ) \gets l \cdot ( n - j ) \cr 7 & \qquad \textbf{until}\ l \ge p\ \textbf{or}\ l \ge 2^{64} \bmod p \cr 8 & \qquad \textbf{for}\ j \gets 0\ \textbf{to}\ k - 1\ \textbf{do} \ \mathrm{swap}( A[ n - 1 - j ] , A[ \mathit{idx}_j ] ) \cr 9 & \qquad n \gets n - k \end{array}} \]
| std::uint64_t ADS::uniformBelow | ( | G && | g, |
| std::uint64_t | s ) |
an integer uniform in \( [ 0 , s ) \), by the method of Lemire
In pseudocode (Lemire, beyond the book), the division only when a rejection is possible:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{uniformBelow}( g , s ) \cr 2 & \quad ( h , l ) \gets \text{the high and the low word of } g() \cdot s \cr 3 & \quad \textbf{if}\ l < s\ \textbf{then} \cr 4 & \qquad t \gets 2^{64} \bmod s \cr 5 & \qquad \textbf{while}\ l < t\ \textbf{do}\ ( h , l ) \gets g() \cdot s \cr 6 & \quad \textbf{return}\ h \end{array}} \]
| double ADS::uniformReal | ( | G && | g | ) |
a double uniform in \( [ 0 , 1 ) \), the top 53 bits of a word times \( 2^{-53} \)