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

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

#include "ADS/Contract.h"
#include "ADS/numeric/NumberTheory.h"
#include <algorithm>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <random>
#include <utility>
#include <vector>
#include "ADS/numeric/impl/RandomNumbers.tpp"

Go to the source code of this file.

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...

Namespaces

namespace  ADS

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:

  • LinearCongruential, \( X_{n+1} = ( a X_n + c ) \bmod m \) (section 3.2.1); with the theorem of Hull and Dobell its period is \( m \) exactly when \( c \) and \( m \) are coprime, \( a - 1 \) is a multiple of every prime factor of \( m \), and of 4 if \( m \) is; minstd() gives the multiplicative generator of Park and Miller, and mmix() that of Knuth for MMIX, \( m = 2^{64} \); its parameters are values at run time, and so are min() and max(), which a UniformRandomBitGenerator has constant: LinearCongruentialT is the same generator with \( a , c , m \) fixed at compile time, which the distributions of the standard library take, MinStd and Mmix those of Park and Miller and of Knuth;
  • chiSquare() and kolmogorovSmirnov() are the two general tests of section 3.3.1: the statistic \( V = \sum ( Y_s - n p_s )^2 / ( n p_s ) \) of the counts in \( k \) categories, to compare with the \( \chi^2 \) distribution with \( k - 1 \) degrees of freedom, and the statistics \( K^+_n , K^-_n \) of the largest deviations of the empirical distribution from the continuous one; serialTest() applies the first to the pairs of consecutive numbers (section 3.3.2B);
  • spectralTest2() is the spectral test in two dimensions (section 3.3.4): the points \( ( X_n , X_{n+1} ) \) of a linear congruential generator lie on parallel lines at the distance \( 1 / \nu_2 \), where \( \nu_2 \) is the length of the shortest nonzero vector \( ( u , v ) \) with \( u + a v \equiv 0 \pmod m \), found by the reduction of Gauss and Lagrange of the basis \( ( m , 0 ) , ( -a , 1 ) \); a good generator has \( \nu_2 \) near \( \sqrt{m} \);
  • shuffle() is Algorithm 3.4.2P (Fisher and Yates, Durstenfeld); selectionSampling() is Algorithm 3.4.2S, \( n \) of \( N \) records in order with one pass, and reservoirSampling() is Algorithm 3.4.2R, \( n \) records from a stream of unknown length.

The contracts [see Contract.h]: \( m \ge 1 \) and \( a , c < m \) for the generator, positive expected counts for the tests, \( n \le N \) for the samplings. The functions are defined in RandomNumbers.tpp.

See also
D. E. Knuth, The Art of Computer Programming, vol. 2, 3rd ed., Addison-Wesley, 1998, chapter 3
T. E. Hull, A. R. Dobell, Random number generators, SIAM Review 4(3), 230-254, 1962, doi:10.1137/1004061
S. K. Park, K. W. Miller, Random number generators: good ones are hard to find, Communications of the ACM 31(10), 1192-1201, 1988, doi:10.1145/63039.63042
R. A. Fisher, F. Yates, Statistical Tables for Biological, Agricultural and Medical Research, Oliver and Boyd, 1938, and R. Durstenfeld, Algorithm 235: random permutation, Communications of the ACM 7(7), 420, 1964, doi:10.1145/364520.364540
J. S. Vitter, Random sampling with a reservoir, ACM Transactions on Mathematical Software 11(1), 37-57, 1985, doi:10.1145/3147.3165
R. P. Brent, An improved Monte Carlo factorization algorithm, BIT Numerical Mathematics 20(2), 176-184, 1980, doi:10.1007/BF01933190 (the detection of the cycle of period())
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa