ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::LinearCongruential Class Reference

the generator \( X_{n+1} = ( a X_n + c ) \bmod m \), \( m = 0 \) meaning \( 2^{64} \) More...

#include <ADS/numeric/RandomNumbers.h>

Public types

using result_type = std::uint64_t
 the type of the numbers

Public member functions

 LinearCongruential (std::uint64_t a, std::uint64_t c, std::uint64_t m, std::uint64_t seed)
 the generator of multiplier \( a \), increment \( c \) and modulus \( m \), from \( X_0 = \mathit{seed} \)
std::uint64_t operator() (void)
 the next number \( X_{n+1} = ( a X_n + c ) \bmod m \), by mulMod() and an addition that does not overflow (section 3.2.1 of Knuth)
std::uint64_t min (void) const
 the least number, 1 if \( c = 0 \) as in std::linear_congruential_engine (0 is then a fixed point, which the multiplicative generators, as minstd(), never reach), 0 otherwise
std::uint64_t max (void) const
 the largest number, \( m - 1 \)
std::uint64_t period (std::uint64_t limit) const
 the period, by walking the sequence, when it is at most \( \mathit{limit} \); 0 otherwise

Static public member functions

static LinearCongruential minstd (std::uint64_t seed=1)
 the generator of Park and Miller, \( a = 16807 \), \( c = 0 \), \( m = 2^{31} - 1 \)
static LinearCongruential mmix (std::uint64_t seed=1)
 the generator of Knuth for MMIX, \( m = 2^{64} \)

Detailed description

the generator \( X_{n+1} = ( a X_n + c ) \bmod m \), \( m = 0 \) meaning \( 2^{64} \)

Member typedef documentation

◆ result_type

using ADS::LinearCongruential::result_type = std::uint64_t

the type of the numbers

Constructor and destructor documentation

◆ LinearCongruential()

ADS::LinearCongruential::LinearCongruential ( std::uint64_t a,
std::uint64_t c,
std::uint64_t m,
std::uint64_t seed )

the generator of multiplier \( a \), increment \( c \) and modulus \( m \), from \( X_0 = \mathit{seed} \)

Precondition
\( m = 0 \), or \( a < m \), \( c < m \) and \( \mathit{seed} < m \)

Member function documentation

◆ max()

std::uint64_t ADS::LinearCongruential::max ( void ) const
inlinenodiscard

the largest number, \( m - 1 \)

◆ min()

std::uint64_t ADS::LinearCongruential::min ( void ) const
inlinenodiscard

the least number, 1 if \( c = 0 \) as in std::linear_congruential_engine (0 is then a fixed point, which the multiplicative generators, as minstd(), never reach), 0 otherwise

◆ minstd()

LinearCongruential ADS::LinearCongruential::minstd ( std::uint64_t seed = 1)
static

the generator of Park and Miller, \( a = 16807 \), \( c = 0 \), \( m = 2^{31} - 1 \)

◆ mmix()

LinearCongruential ADS::LinearCongruential::mmix ( std::uint64_t seed = 1)
static

the generator of Knuth for MMIX, \( m = 2^{64} \)

◆ operator()()

std::uint64_t ADS::LinearCongruential::operator() ( void )

the next number \( X_{n+1} = ( a X_n + c ) \bmod m \), by mulMod() and an addition that does not overflow (section 3.2.1 of Knuth)

◆ period()

std::uint64_t ADS::LinearCongruential::period ( std::uint64_t limit) const
nodiscard

the period, by walking the sequence, when it is at most \( \mathit{limit} \); 0 otherwise

In pseudocode (the cycle detection of Brent, beyond the book), the tortoise moved to the hare at each power of 2:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{period}( \mathit{limit} ) \cr 2 & \quad t \gets X_1;\ h \gets X_2;\ \mathit{power} \gets 1;\ \lambda \gets 1 \cr 3 & \quad \textbf{while}\ t \ne h\ \textbf{do} \cr 4 & \qquad \textbf{if}\ \mathit{power} = \lambda\ \textbf{then}\ t \gets h;\ \mathit{power} \gets 2 \mathit{power};\ \lambda \gets 0 \cr 5 & \qquad h \gets \text{the next number};\ \lambda \gets \lambda + 1 \cr 6 & \qquad \textbf{if}\ \lambda > \mathit{limit}\ \textbf{then return} \ 0 \cr 7 & \quad \textbf{return}\ \lambda \end{array}} \]


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