Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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 <cassert>
17#include <cstdint>
18#include <string>
19#include <vector>
20
22public:
23 virtual double Rndm() = 0;
24 virtual ~TRandomEngine() {}
25};
26
27
28namespace ROOT {
29
30 namespace Math {
31
32
33 class LCGEngine : public TRandomEngine {
34
35
36 public:
37
39 typedef uint32_t Result_t;
40 typedef uint32_t StateInt_t;
41
42 LCGEngine() : fSeed(65539) { }
43
44 ~LCGEngine() override {}
45
46 void SetSeed(uint32_t seed) { fSeed = seed; }
47
48 double Rndm() override {
49 //double Rndm() {
50 return Rndm_impl();
51 }
52 inline double operator() () { return Rndm_impl(); }
53
54 uint32_t IntRndm() {
55 fSeed = (1103515245 * fSeed + 12345) & 0x7fffffffUL;
56 return fSeed;
57 }
58
59 /// minimum integer that can be generated
60 static unsigned int MinInt() { return 0; }
61 /// maximum integer that can be generated
62 static unsigned int MaxInt() { return 0xffffffff; } // 2^32 -1
63 /// Size of the generator state
64 static int Size() { return 1; }
65 /// Name of the generator
66 static std::string Name() { return "LCGEngine"; }
67 protected:
68 // for testing all generators
69 void SetState(const std::vector<uint32_t> & state) {
70 assert(!state.empty());
71 fSeed = state[0];
72 }
73
74 void GetState(std::vector<uint32_t> & state) {
75 state.resize(1);
76 state[0] = fSeed;
77 }
78 int Counter() const { return 0; }
79 private:
80
81 double Rndm_impl() {
82 const double kCONS = 4.6566128730774E-10; // (1/pow(2,31)
83 unsigned int rndm = IntRndm(); // generate integer number
84 if (rndm != 0) return kCONS*rndm;
85 return Rndm_impl();
86 }
87
88 uint32_t fSeed;
89 };
90
91
92 } // end namespace Math
93
94} // end namespace ROOT
95
96
97#endif /* ROOT_Math_LCGEngine */
static unsigned int MaxInt()
maximum integer that can be generated
Definition LCGEngine.h:62
~LCGEngine() override
Definition LCGEngine.h:44
double Rndm() override
Definition LCGEngine.h:48
static unsigned int MinInt()
minimum integer that can be generated
Definition LCGEngine.h:60
int Counter() const
Definition LCGEngine.h:78
TRandomEngine BaseType
Definition LCGEngine.h:38
void GetState(std::vector< uint32_t > &state)
Definition LCGEngine.h:74
static std::string Name()
Name of the generator.
Definition LCGEngine.h:66
static int Size()
Size of the generator state.
Definition LCGEngine.h:64
void SetState(const std::vector< uint32_t > &state)
Definition LCGEngine.h:69
void SetSeed(uint32_t seed)
Definition LCGEngine.h:46
virtual double Rndm()=0
virtual ~TRandomEngine()
Definition LCGEngine.h:24
Namespace for new Math classes and functions.
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...