Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-03-13 09:25:06

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_MersenneTwisterEngine
0014 #define ROOT_Math_MersenneTwisterEngine
0015 
0016 #include "Math/TRandomEngine.h"
0017 
0018 #include <cstdint>
0019 #include <vector>
0020 #include <string>
0021 
0022 namespace ROOT {
0023 
0024    namespace Math {
0025 
0026       /**
0027          Random number generator class based on
0028          M. Matsumoto and T. Nishimura,
0029          Mersenne Twister: A 623-dimensionally equidistributed
0030          uniform pseudorandom number generator
0031          ACM Transactions on Modeling and Computer Simulation,
0032          Vol. 8, No. 1, January 1998, pp 3--30.
0033 
0034          For more information see the Mersenne Twister homepage
0035          [http://www.math.keio.ac.jp/~matumoto/emt.html]
0036 
0037          Advantage:
0038 
0039          -  large period 2**19937 -1
0040          -  relatively fast (slightly slower than TRandom1 and TRandom2 but much faster than TRandom1)
0041 
0042          Note that this is a 32 bit implementation. Only 32 bits of the returned double numbers are random.
0043          in case more precision is needed, one should use an engine providing at least 48 random bits.
0044 
0045          Drawback:  a relative large internal state of 624 integers
0046 
0047          @ingroup Random
0048       */
0049 
0050       class MersenneTwisterEngine : public TRandomEngine {
0051 
0052 
0053       public:
0054 
0055          typedef  TRandomEngine BaseType;
0056          typedef  uint32_t Result_t;
0057          typedef  uint32_t StateInt_t;
0058 
0059 
0060          MersenneTwisterEngine(uint32_t seed=4357)  {
0061             SetSeed(seed);
0062          }
0063 
0064          ~MersenneTwisterEngine() override {}
0065 
0066          void SetSeed(Result_t seed);
0067 
0068          double Rndm() override {
0069             return Rndm_impl();
0070          }
0071          inline double operator() () { return Rndm_impl(); }
0072 
0073          uint32_t IntRndm() {
0074             return IntRndm_impl();
0075          }
0076 
0077          /// minimum integer that can be generated
0078          static unsigned int MinInt() { return 0; }
0079          /// maximum integer that can be generated
0080          static unsigned int MaxInt() { return 0xffffffff; }  //  2^32 -1
0081 
0082          static int Size() { return kSize; }
0083 
0084          static std::string Name() {
0085             return "MersenneTwisterEngine";
0086          }
0087 
0088       protected:
0089          // functions used for testing
0090 
0091          void SetState(const std::vector<uint32_t> & state) {
0092             for (unsigned int i = 0; i < kSize; ++i)
0093                fMt[i] = state[i];
0094             fCount624 = kSize; // to make sure we re-iterate on the new state
0095          }
0096 
0097          void GetState(std::vector<uint32_t> & state) {
0098             state.resize(kSize);
0099             for (unsigned int i = 0; i < kSize; ++i)
0100                state[i] = fMt[i];
0101          }
0102 
0103          int Counter() const { return fCount624; }
0104 
0105 
0106       private:
0107 
0108          double Rndm_impl();
0109          uint32_t IntRndm_impl();
0110 
0111          enum {
0112             kSize=624
0113          };
0114          uint32_t  fMt[kSize];
0115          int fCount624;
0116       };
0117 
0118 
0119    } // end namespace Math
0120 
0121 } // end namespace ROOT
0122 
0123 
0124 #endif /* ROOT_Math_TRandomEngines */