Line data Source code
1 : /////////////////////////////////////////////////////////////////////////// 2 : // Spetabaru - Berenger Bramas MPCDF - 2017 3 : // Under LGPL Licence, please you must read the LICENCE file. 4 : /////////////////////////////////////////////////////////////////////////// 5 : #ifndef SPMTGENERATOR_HPP 6 : #define SPMTGENERATOR_HPP 7 : 8 : #include <random> 9 : 10 : /** 11 : * The is a random generator based on mt19937. 12 : * It supports skipping values, but it is simply an emulation, 13 : * because to skip N values, the generator calls N times rand. 14 : * Therefore, it is adequate for testing but should not be used 15 : * when performance is important. 16 : */ 17 : template <class RealType = double> 18 : class SpMTGenerator { 19 : std::mt19937_64 mtEngine; 20 : std::uniform_real_distribution<RealType> dis01; 21 : std::size_t nbValuesGenerated; 22 : 23 : public: 24 : explicit SpMTGenerator() : mtEngine(std::random_device()()), dis01(0,1), nbValuesGenerated(0){} 25 : 26 584 : explicit SpMTGenerator(const size_t inSeed) : mtEngine(inSeed), dis01(0,1), nbValuesGenerated(0){} 27 : 28 : SpMTGenerator(const SpMTGenerator&) = default; 29 : SpMTGenerator(SpMTGenerator&&) = default; 30 : SpMTGenerator& operator=(const SpMTGenerator&) = default; 31 : SpMTGenerator& operator=(SpMTGenerator&&) = default; 32 : 33 207284 : SpMTGenerator& skip(const size_t inNbToSeep){ 34 : // To skip a value, we call rand() and do not use 35 : // the result of the call. 36 3969996 : for(size_t idx = 0 ; idx < inNbToSeep ; ++idx){ 37 3762712 : getRand01(); 38 : } 39 207284 : return *this; 40 : } 41 : 42 4050536 : RealType getRand01(){ 43 4050536 : nbValuesGenerated += 1; 44 4050536 : return dis01(mtEngine); 45 : } 46 : 47 : size_t getNbValuesGenerated() const{ 48 : return nbValuesGenerated; 49 : } 50 : }; 51 : 52 : #endif