Logo ROOT   6.08/07
Reference Guide
LCGEngine.h
Go to the documentation of this file.
1 // @(#)root/mathcore:$Id$
2 // Author: L. Moneta Tue Aug 4 2015
3 
4 /**********************************************************************
5  * *
6  * Copyright (c) 2015 LCG ROOT Math Team, CERN/PH-SFT *
7  * *
8  * *
9  **********************************************************************/
10 
11 // random engines based on ROOT
12 
13 #ifndef ROOT_Math_LCGEngine
14 #define ROOT_Math_LCGEngine
15 
16 #include <cstdint>
17 #include <vector>
18 #include <cassert>
19 
21 public:
22  virtual double Rndm() = 0;
23  virtual ~TRandomEngine() {}
24 };
25 
26 
27 namespace ROOT {
28 
29  namespace Math {
30 
31 
32  class LCGEngine : public TRandomEngine {
33 
34 
35  public:
36 
38  typedef uint32_t Result_t;
39  typedef uint32_t StateInt_t;
40 
41  LCGEngine() : fSeed(65539) { }
42 
43  virtual ~LCGEngine() {}
44 
45  void SetSeed(uint32_t seed) { fSeed = seed; }
46 
47  virtual double Rndm() {
48  //double Rndm() {
49  return Rndm_impl();
50  }
51  inline double operator() () { return Rndm_impl(); }
52 
53  uint32_t IntRndm() {
54  fSeed = (1103515245 * fSeed + 12345) & 0x7fffffffUL;
55  return fSeed;
56  }
57 
58  /// minimum integer taht can be generated
59  static unsigned int MinInt() { return 0; }
60  /// maximum integer taht can be generated
61  static unsigned int MaxInt() { return 0xffffffff; } // 2^32 -1
62  /// Size of the generator state
63  static int Size() { return 1; }
64  /// Name of the generator
65  static std::string Name() { return "LCGEngine"; }
66  protected:
67  // for testing all generators
68  void SetState(const std::vector<uint32_t> & state) {
69  assert(!state.empty());
70  fSeed = state[0];
71  }
72 
73  void GetState(std::vector<uint32_t> & state) {
74  state.resize(1);
75  state[0] = fSeed;
76  }
77  int Counter() const { return 0; }
78  private:
79 
80  double Rndm_impl() {
81  const double kCONS = 4.6566128730774E-10; // (1/pow(2,31)
82  unsigned int rndm = IntRndm(); // generate integer number
83  if (rndm != 0) return kCONS*rndm;
84  return Rndm_impl();
85  }
86 
87  uint32_t fSeed;
88  };
89 
90 
91  } // end namespace Math
92 
93 } // end namespace ROOT
94 
95 
96 #endif /* ROOT_Math_LCGEngine */
uint32_t IntRndm()
Definition: LCGEngine.h:53
This namespace contains pre-defined functions to be used in conjuction with TExecutor::Map and TExecu...
Definition: StringConv.hxx:21
virtual ~LCGEngine()
Definition: LCGEngine.h:43
virtual double Rndm()
Definition: LCGEngine.h:47
void GetState(std::vector< uint32_t > &state)
Definition: LCGEngine.h:73
void SetSeed(uint32_t seed)
Definition: LCGEngine.h:45
virtual double Rndm()=0
void SetState(const std::vector< uint32_t > &state)
Definition: LCGEngine.h:68
static int Size()
Size of the generator state.
Definition: LCGEngine.h:63
TRandomEngine BaseType
Definition: LCGEngine.h:37
static unsigned int MaxInt()
maximum integer taht can be generated
Definition: LCGEngine.h:61
virtual ~TRandomEngine()
Definition: LCGEngine.h:23
static unsigned int MinInt()
minimum integer taht can be generated
Definition: LCGEngine.h:59
Namespace for new Math classes and functions.
int Counter() const
Definition: LCGEngine.h:77
static std::string Name()
Name of the generator.
Definition: LCGEngine.h:65