Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 09:14:04

0001 // @(#)root/mathcore:$Id$
0002 // Author: L. Moneta Tue Aug 4 2015
0003 
0004 /**********************************************************************
0005  *                                                                    *
0006  * Copyright (c) 2015  LCG ROOT Math Team, CERN/PH-SFT                *
0007  *                                                                    *
0008  *                                                                    *
0009  **********************************************************************/
0010 
0011 // random engines based on ROOT
0012 
0013 #ifndef ROOT_Math_StdEngine
0014 #define ROOT_Math_StdEngine
0015 
0016 #include <cstdint>
0017 #include <random>
0018 
0019 namespace ROOT {
0020 
0021    namespace Math {
0022 
0023 
0024       class StdRandomEngine {};
0025 
0026       template<class Generator>
0027       struct StdEngineType {
0028          static const char *  Name() { return "std_random_eng";}
0029       };
0030       template<>
0031          struct StdEngineType<std::minstd_rand> {
0032          static const char *  Name() { return "std_minstd_rand";}
0033       };
0034       template<>
0035       struct StdEngineType<std::mt19937> {
0036          static const char *  Name() { return "std_mt19937";}
0037       };
0038       template<>
0039       struct StdEngineType<std::mt19937_64> {
0040          static const char *  Name() { return "std_mt19937_64";}
0041       };
0042       template<>
0043       struct StdEngineType<std::ranlux24> {
0044          static const char *  Name() { return "std_ranlux24";}
0045       };
0046       template<>
0047       struct StdEngineType<std::ranlux48> {
0048          static const char *  Name() { return "std_ranlux48";}
0049       };
0050       template<>
0051       struct StdEngineType<std::knuth_b> {
0052          static const char *  Name() { return "std_knuth_b";}
0053       };
0054       template<>
0055       struct StdEngineType<std::random_device> {
0056          static const char *  Name() { return "std_random_device";}
0057       };
0058 
0059 
0060       /**
0061           @ingroup Random
0062           Class to wrap engines from the C++ standard random library in
0063           the ROOT Random interface.
0064           These cases are then used by the generic TRandomGen class
0065           to provide TRandom interrace generators for the C++ random generators.
0066 
0067           See for examples the TRandomMT64 and TRandomRanlux48 generators
0068           which are typede's to TRandomGen instantiated with some
0069           random engine from the C++ standard library.
0070 
0071       */
0072 
0073       template <class Generator>
0074       class StdEngine {
0075 
0076 
0077       public:
0078 
0079          typedef  StdRandomEngine BaseType;
0080          typedef  typename Generator::result_type Result_t;
0081 
0082          StdEngine() : fGen() {
0083             fCONS = 1./fGen.max();
0084          }
0085 
0086 
0087          void SetSeed(Result_t seed) { fGen.seed(seed);}
0088 
0089          double Rndm() {
0090             Result_t rndm = fGen(); // generate integer number according to the type
0091             if (rndm != 0) return  fCONS*rndm;
0092             return Rndm();
0093          }
0094 
0095          Result_t IntRndm() {
0096             return fGen();
0097          }
0098 
0099          double operator() () {
0100             return Rndm();
0101          }
0102 
0103          static const char * Name()  {
0104             return StdEngineType<Generator>::Name();
0105          }
0106 
0107          static uint64_t MaxInt() { return Generator::max(); }
0108 
0109 
0110       private:
0111          Generator fGen;
0112          double fCONS;   //! cached value of maximum integer value generated
0113       };
0114 
0115 
0116       extern template class StdEngine<std::mt19937_64>;
0117       extern template class StdEngine<std::ranlux48>;
0118 
0119    } // end namespace Math
0120 
0121 } // end namespace ROOT
0122 
0123 
0124 #endif /* ROOT_Math_StdEngine */