ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
RandomNumbers.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*-------------------------- File RandomNumbers.h --------------------------*/
3/*--------------------------------------------------------------------------*/
68/*--------------------------------------------------------------------------*/
69/*----------------------------- DEFINITIONS --------------------------------*/
70/*--------------------------------------------------------------------------*/
71
72#ifndef __ADS_RandomNumbers
73 #define __ADS_RandomNumbers
74/* self-identification: #endif constant */
75
76/*--------------------------------------------------------------------------*/
77/*------------------------------ INCLUDES ----------------------------------*/
78/*--------------------------------------------------------------------------*/
79
80#include "ADS/Contract.h"
82
83#include <algorithm>
84#include <cmath>
85#include <cstddef>
86#include <cstdint>
87#include <limits>
88#include <random>
89#include <utility>
90#include <vector>
91
92/*--------------------------------------------------------------------------*/
93/*--------------------------- NAMESPACE ------------------------------------*/
94/*--------------------------------------------------------------------------*/
95
96namespace ADS
97{
98
99/*--------------------------------------------------------------------------*/
100/*------------------------ CLASS LinearCongruential ------------------------*/
101/*--------------------------------------------------------------------------*/
102
106{
107 public:
108 using result_type = std::uint64_t;
109
114 LinearCongruential( std::uint64_t a , std::uint64_t c , std::uint64_t m ,
115 std::uint64_t seed );
116
119 static LinearCongruential minstd( std::uint64_t seed = 1 );
120
122 static LinearCongruential mmix( std::uint64_t seed = 1 );
123
127 std::uint64_t operator()( void );
128
132 [[nodiscard]] std::uint64_t min( void ) const { return( f_c == 0 ); }
134 [[nodiscard]] std::uint64_t max( void ) const {
135 return( f_m ? f_m - 1 : std::numeric_limits< std::uint64_t >::max() );
136 }
137
157 [[nodiscard]] std::uint64_t period( std::uint64_t limit ) const;
158
159 private:
160 std::uint64_t f_a ,
161 f_c ,
162 f_m ,
163 f_x;
164 };
165
166/*--------------------------------------------------------------------------*/
167/*----------------------- CLASS LinearCongruentialT ------------------------*/
168/*--------------------------------------------------------------------------*/
169
174template< std::uint64_t a , std::uint64_t c , std::uint64_t m >
176{
177 static_assert( ( m == 0 ) || ( ( a < m ) && ( c < m ) ) , "a , c < m" );
178
179 public:
180 using result_type = std::uint64_t;
181
184 explicit constexpr LinearCongruentialT( std::uint64_t seed = 1 );
185
187 constexpr std::uint64_t operator()( void );
188
190 static constexpr std::uint64_t min( void ) { return( c == 0 ); }
192 static constexpr std::uint64_t max( void ) {
193 return( m ? m - 1 : std::numeric_limits< std::uint64_t >::max() );
194 }
195
197 bool operator==( const LinearCongruentialT & ) const = default;
198
199 private:
200 std::uint64_t f_x;
201 };
202
206
208using Mmix =
210
223inline double chiSquare( const std::vector< long > & Y ,
224 const std::vector< double > & p );
225
242inline std::pair< double , double >
243kolmogorovSmirnov( std::vector< double > U );
244
260inline double serialTest( const std::vector< double > & U , int d );
261
281inline double spectralTest2( std::uint64_t a , std::uint64_t m );
282
299template< class T , class R >
300void shuffle( std::vector< T > & A , R & g );
301
320template< class R >
321std::vector< std::size_t > selectionSampling( std::size_t n , std::size_t N ,
322 R & g );
323
341template< class T , class R >
342std::vector< T > reservoirSampling( const std::vector< T > & S ,
343 std::size_t n ,
344 R & g );
345
346/*--------------------------------------------------------------------------*/
347
348} // end( namespace ADS )
349
350/*--------------------------------------------------------------------------*/
351/*-------------------------- IMPLEMENTATION --------------------------------*/
352/*--------------------------------------------------------------------------*/
353
354#include "ADS/numeric/impl/RandomNumbers.tpp"
355
356/*--------------------------------------------------------------------------*/
357
358#endif /* RandomNumbers.h included */
359
360/*--------------------------------------------------------------------------*/
361/*------------------------ End File RandomNumbers.h ------------------------*/
362/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The arithmetic of section 17.2 and of exercises 17.1-17.3, on unsigned integers of 64 bits:
the generator of LinearCongruential with fixed at compile time, meaning : a UniformRandomBitGenera...
Definition RandomNumbers.h:176
std::uint64_t result_type
the type of the numbers
Definition RandomNumbers.h:180
constexpr LinearCongruentialT(std::uint64_t seed=1)
the generator from
constexpr std::uint64_t operator()(void)
the next number,
static constexpr std::uint64_t max(void)
the largest number,
Definition RandomNumbers.h:192
bool operator==(const LinearCongruentialT &) const =default
whether the two generators are in the same state
static constexpr std::uint64_t min(void)
the least number, 1 if , 0 otherwise
Definition RandomNumbers.h:190
std::uint64_t result_type
the type of the numbers
Definition RandomNumbers.h:108
std::uint64_t min(void) const
the least number, 1 if as in std::linear_congruential_engine (0 is then a fixed point,...
Definition RandomNumbers.h:132
static LinearCongruential mmix(std::uint64_t seed=1)
the generator of Knuth for MMIX,
std::uint64_t period(std::uint64_t limit) const
the period, by walking the sequence, when it is at most ; 0 otherwise
std::uint64_t max(void) const
the largest number,
Definition RandomNumbers.h:134
LinearCongruential(std::uint64_t a, std::uint64_t c, std::uint64_t m, std::uint64_t seed)
the generator of multiplier , increment and modulus , from
std::uint64_t operator()(void)
the next number , by mulMod() and an addition that does not overflow (section 3.2....
static LinearCongruential minstd(std::uint64_t seed=1)
the generator of Park and Miller, , ,
double chiSquare(const std::vector< long > &Y, const std::vector< double > &p)
the chi-square statistic of the counts against the probabilities over samples
std::vector< T > reservoirSampling(const std::vector< T > &S, std::size_t n, R &g)
items from the sequence , as if it were a stream of unknown length; all of , in its order,...
double serialTest(const std::vector< double > &U, int d)
the chi-square statistic of the pairs of numbers in in cells, degrees of freedom
void shuffle(std::vector< T > &A, R &g)
the vector shuffled by , every permutation equally likely if is uniform on its range
std::pair< double, double > kolmogorovSmirnov(std::vector< double > U)
the statistics of Kolmogorov and Smirnov of the numbers in against the uniform distribution,...
LinearCongruentialT< 16807, 0, 2147483647 > MinStd
the generator of Park and Miller, , ,
Definition RandomNumbers.h:205
LinearCongruentialT< 6364136223846793005ULL, 1442695040888963407ULL, 0 > Mmix
the generator of Knuth for MMIX,
Definition RandomNumbers.h:208
std::vector< std::size_t > selectionSampling(std::size_t n, std::size_t N, R &g)
of the items , in order, by one pass
double spectralTest2(std::uint64_t a, std::uint64_t m)
the of the spectral test of the generator of multiplier and modulus
Definition ADT.h:96