Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:10:18

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_LCGEngine
0014 #define ROOT_Math_LCGEngine
0015 
0016 #include <cassert>
0017 #include <cstdint>
0018 #include <string>
0019 #include <vector>
0020 
0021 class TRandomEngine  {
0022 public:
0023    virtual double Rndm() = 0;
0024    virtual ~TRandomEngine() {}
0025 };
0026 
0027 
0028 namespace ROOT {
0029 
0030    namespace Math {
0031 
0032       
0033       class LCGEngine : public TRandomEngine {
0034 
0035 
0036       public:
0037 
0038          typedef  TRandomEngine BaseType;
0039          typedef  uint32_t Result_t;
0040          typedef  uint32_t StateInt_t;
0041 
0042          LCGEngine() : fSeed(65539) { }
0043 
0044          ~LCGEngine() override {}
0045 
0046          void SetSeed(uint32_t seed) { fSeed = seed; }
0047 
0048          double Rndm() override {
0049             //double Rndm() {
0050             return Rndm_impl();
0051          }
0052          inline double operator() () { return Rndm_impl(); }
0053 
0054          uint32_t IntRndm() {
0055             fSeed = (1103515245 * fSeed + 12345) & 0x7fffffffUL;
0056             return fSeed; 
0057          }
0058 
0059          /// minimum integer that can be generated
0060          static unsigned int MinInt() { return 0; }
0061          /// maximum integer that can be generated
0062          static unsigned int MaxInt() { return 0xffffffff; }  //  2^32 -1
0063          /// Size of the generator state
0064          static int Size() { return 1; }
0065          /// Name of the generator
0066          static std::string Name() { return "LCGEngine"; }
0067       protected:
0068          // for testing all generators
0069          void SetState(const std::vector<uint32_t> & state) {
0070             assert(!state.empty());
0071             fSeed = state[0]; 
0072          }
0073 
0074          void GetState(std::vector<uint32_t> & state) {
0075             state.resize(1);
0076             state[0] = fSeed;
0077          }
0078          int Counter() const { return 0; }         
0079       private:
0080 
0081          double Rndm_impl() {
0082             const double kCONS = 4.6566128730774E-10; // (1/pow(2,31)
0083             unsigned int rndm = IntRndm(); // generate integer number 
0084             if (rndm != 0) return  kCONS*rndm;
0085             return Rndm_impl();
0086          }
0087          
0088          uint32_t fSeed;
0089       };
0090       
0091 
0092    } // end namespace Math
0093 
0094 } // end namespace ROOT
0095 
0096 
0097 #endif /* ROOT_Math_LCGEngine */