ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
RandomNumbers.h

Random numbers as Knuth treats them (chapter 3 of The Art of Computer Programming), beyond the book. More...

Files

file  ADS/numeric/RandomNumbers.h
 Random numbers as Knuth treats them (chapter 3 of The Art of Computer Programming), beyond the book:

Classes

class  ADS::LinearCongruential
 the generator \( X_{n+1} = ( a X_n + c ) \bmod m \), \( m = 0 \) meaning \( 2^{64} \) More...
class  ADS::LinearCongruentialT< a, c, m >
 the generator \( X_{n+1} = ( a X_n + c ) \bmod m \) of LinearCongruential with \( a , c , m \) fixed at compile time, \( m = 0 \) meaning \( 2^{64} \): a UniformRandomBitGenerator, as min() and max() are constant More...

Typedefs

using ADS::MinStd = LinearCongruentialT< 16807 , 0 , 2147483647 >
 the generator of Park and Miller, \( a = 16807 \), \( c = 0 \), \( m = 2^{31} - 1 \)
using ADS::Mmix
 the generator of Knuth for MMIX, \( m = 2^{64} \)

Functions

double ADS::chiSquare (const std::vector< long > &Y, const std::vector< double > &p)
 the chi-square statistic \( V \) of the counts \( Y \) against the probabilities \( p \) over \( n = \sum_s Y_s \) samples
std::pair< double, double > ADS::kolmogorovSmirnov (std::vector< double > U)
 the statistics \( ( K^+_n , K^-_n ) \) of Kolmogorov and Smirnov of the numbers \( U \) in \( [ 0 , 1 ) \) against the uniform distribution, scaled by \( \sqrt{n} \)
double ADS::serialTest (const std::vector< double > &U, int d)
 the chi-square statistic of the pairs \( ( U_{2j} , U_{2j+1} ) \) of numbers in \( [ 0 , 1 ) \) in \( d \times d \) cells, \( d^2 - 1 \) degrees of freedom
double ADS::spectralTest2 (std::uint64_t a, std::uint64_t m)
 the \( \nu_2 \) of the spectral test of the generator of multiplier \( a \) and modulus \( m \)
template<class T, class R>
void ADS::shuffle (std::vector< T > &A, R &g)
 the vector \( A \) shuffled by \( g \), every permutation equally likely if \( g \) is uniform on its range
template<class R>
std::vector< std::size_t > ADS::selectionSampling (std::size_t n, std::size_t N, R &g)
 \( n \) of the \( N \) items \( 0 , \ldots , N - 1 \), in order, by one pass
template<class T, class R>
std::vector< T > ADS::reservoirSampling (const std::vector< T > &S, std::size_t n, R &g)
 \( n \) items from the sequence \( S \), as if it were a stream of unknown length; all of \( S \), in its order, when \( S \) has at most \( n \) items (a stream shorter than the sample)

Detailed description

Random numbers as Knuth treats them (chapter 3 of The Art of Computer Programming), beyond the book.

The definitions, the invariants and the costs of the area are in Theory of numerical algorithms.

Typedef documentation

◆ MinStd

using ADS::MinStd = LinearCongruentialT< 16807 , 0 , 2147483647 >

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

◆ Mmix

using ADS::Mmix
Initial value:
the generator of LinearCongruential with fixed at compile time, meaning : a UniformRandomBitGenera...
Definition RandomNumbers.h:176

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

Function documentation

◆ chiSquare()

double ADS::chiSquare ( const std::vector< long > & Y,
const std::vector< double > & p )
inline

the chi-square statistic \( V \) of the counts \( Y \) against the probabilities \( p \) over \( n = \sum_s Y_s \) samples

In pseudocode (section 3.3.1 of Knuth, beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{chiSquare}( Y , p ) \cr 2 & \quad n \gets \sum_s Y_s \cr 3 & \quad \textbf{return}\ \sum_s ( Y_s - n p_s )^2 / ( n p_s ) \end{array}} \]

Precondition
\( Y \) and \( p \) of the same size, each \( n p_s > 0 \)

◆ kolmogorovSmirnov()

std::pair< double, double > ADS::kolmogorovSmirnov ( std::vector< double > U)
inline

the statistics \( ( K^+_n , K^-_n ) \) of Kolmogorov and Smirnov of the numbers \( U \) in \( [ 0 , 1 ) \) against the uniform distribution, scaled by \( \sqrt{n} \)

In pseudocode (section 3.3.1 of Knuth, beyond the book), on the numbers sorted:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{kolmogorovSmirnov}( U ) \cr 2 & \quad \text{sort } U \text{ into } x_1 \le \cdots \le x_n \cr 3 & \quad K^+ \gets \sqrt{n} \max_j ( j / n - x_j );\ K^- \gets \sqrt{n} \max_j ( x_j - ( j - 1 ) / n ) \cr 4 & \quad \textbf{return}\ ( K^+ , K^- ) \end{array}} \]

Precondition
\( U \) not empty

◆ reservoirSampling()

template<class T, class R>
std::vector< T > ADS::reservoirSampling ( const std::vector< T > & S,
std::size_t n,
R & g )

\( n \) items from the sequence \( S \), as if it were a stream of unknown length; all of \( S \), in its order, when \( S \) has at most \( n \) items (a stream shorter than the sample)

In pseudocode (Algorithm 3.4.2R of Knuth, beyond the book), the item \( t \) entering with probability \( n / ( t + 1 ) \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{reservoirSampling}( S , n , g ) \cr 2 & \quad \textbf{for}\ t \gets 0\ \textbf{to}\ |S| - 1\ \textbf{do} \cr 3 & \qquad \textbf{if}\ t < n\ \textbf{then}\ \text{append } S[ t ] \text{ to } R \cr 4 & \qquad \textbf{else}\ M \gets g() \bmod ( t + 1 );\ \textbf{if}\ M < n \ \textbf{then}\ R[ M ] \gets S[ t ] \cr 5 & \quad \textbf{return}\ R \end{array}} \]

◆ selectionSampling()

template<class R>
std::vector< std::size_t > ADS::selectionSampling ( std::size_t n,
std::size_t N,
R & g )

\( n \) of the \( N \) items \( 0 , \ldots , N - 1 \), in order, by one pass

In pseudocode (Algorithm 3.4.2S of Knuth, beyond the book), the item \( t \) taken with probability \( ( n - m ) / ( N - t ) \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{selectionSampling}( n , N , g ) \cr 2 & \quad O \gets \text{the empty list};\ t \gets 0 \cr 3 & \quad \textbf{while}\ |O| < n\ \textbf{do} \cr 4 & \qquad U \gets \text{a uniform of } [ 0 , 1 ) \cr 5 & \qquad \textbf{if}\ ( N - t ) U < n - |O|\ \textbf{then} \ \text{append } t \text{ to } O \cr 6 & \qquad t \gets t + 1 \cr 7 & \quad \textbf{return}\ O \end{array}} \]

Precondition
\( n \le N \)

◆ serialTest()

double ADS::serialTest ( const std::vector< double > & U,
int d )
inline

the chi-square statistic of the pairs \( ( U_{2j} , U_{2j+1} ) \) of numbers in \( [ 0 , 1 ) \) in \( d \times d \) cells, \( d^2 - 1 \) degrees of freedom

In pseudocode (section 3.3.2B of Knuth, beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{serialTest}( U , d ) \cr 2 & \quad Y \gets d^2 \text{ zero counts} \cr 3 & \quad \textbf{foreach}\ j\ \textbf{do}\ Y[ \lfloor d U_{2j} \rfloor , \lfloor d U_{2j+1} \rfloor ] \gets Y[ \ldots ] + 1 \cr 4 & \quad \textbf{return}\ \mathrm{chiSquare}( Y , \text{each } 1 / d^2 ) \end{array}} \]

Precondition
\( d \ge 2 \) and at least 2 numbers

◆ shuffle()

template<class T, class R>
void ADS::shuffle ( std::vector< T > & A,
R & g )

the vector \( A \) shuffled by \( g \), every permutation equally likely if \( g \) is uniform on its range

In pseudocode (Algorithm 3.4.2P of Knuth, beyond the book), each \( k \) uniform in \( [ 0 , j ) \) by the method of Lemire on a full word [see uniformBelow() of RandomGenerators.h] and by rejection on a narrower range of \( g \), never by \( g() \bmod j \), which favors the small residues:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{shuffle}( A , g ) \cr 2 & \quad \textbf{for}\ j \gets |A|\ \textbf{downto}\ 2\ \textbf{do} \cr 3 & \qquad k \gets \text{an integer uniform in } [ 0 , j ); \ \mathrm{swap}( A[ j - 1 ] , A[ k ] ) \end{array}} \]

◆ spectralTest2()

double ADS::spectralTest2 ( std::uint64_t a,
std::uint64_t m )
inline

the \( \nu_2 \) of the spectral test of the generator of multiplier \( a \) and modulus \( m \)

In pseudocode (the reduction of Gauss and Lagrange, section 3.3.4 of Knuth, beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{spectralTest2}( a , m ) \cr 2 & \quad b_1 \gets ( m , 0 );\ b_2 \gets ( -a , 1 ) \cr 3 & \quad \textbf{loop} \cr 4 & \qquad \textbf{if}\ \| b_1 \| < \| b_2 \|\ \textbf{then} \ \mathrm{swap}( b_1 , b_2 ) \cr 5 & \qquad b_1 \gets b_1 - \lfloor b_1 \cdot b_2 / \| b_2 \|^2 \rceil b_2 \cr 6 & \qquad \textbf{if}\ \| b_1 \| \ge \| b_2 \|\ \textbf{then return} \ \| b_2 \| \end{array}} \]

Precondition
\( m \ge 2 \) and \( 0 < a < m \)