Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
MixMaxEngine.icc
Go to the documentation of this file.
1 // @(#)root/mathcore:$Id$
2 // Authors: L. Moneta 8/2015
3 
4 /**********************************************************************
5  * *
6  * Copyright (c) 2015 , ROOT MathLib Team *
7  * *
8  * *
9  **********************************************************************/
10 
11 // implementation file of MixMax engine
12 //
13 //
14 // Created by: Lorenzo Moneta : Tue 4 Aug 2015
15 //
16 //
17 #ifndef ROOT_Math_MixMaxEngine_icc
18 #define ROOT_Math_MixMaxEngine_icc
19 
20 #ifndef ROOT_Math_MixMaxEngine
21 #error "Do not use MixMaxEngine.icc directly. #include \"MixMaxEngine.h\" instead."
22 #endif // ROOT_Math_MixMaxEngine
23 
24 //#include "Math/mixmax/mixmax.h"
25 #include <cassert>
26 #include "Math/Util.h"
27 
28 
29 namespace ROOT {
30 namespace Math {
31 
32 
33 
34  template<int N, int S>
35  MixMaxEngine<N,S>::MixMaxEngine(uint64_t seed) {
36  // fRng = new mixmax::mixmax_engine<N>();
37  // fRng->seed(seed);
38  fRng = new MixMaxEngineImpl<N>(seed);
39  }
40 
41  template<int N, int S>
42  MixMaxEngine<N,S>::~MixMaxEngine() {
43  if (fRng) delete fRng;
44  }
45 
46 
47  // void template<int N, int S>
48  //MixMaxEngine<N,S>::SeedUniqueStream(unsigned int clusterID, unsigned int machineID, unsigned int runID, unsigned int streamID) {
49  // seed_uniquestream(fRngState, clusterID, machineID, runID, streamID);
50  // }
51 
52  template<int N, int S>
53  void MixMaxEngine<N,S>::SetSeed(uint64_t seed) {
54  //fRng->seed(seed);
55  fRng->SetSeed(seed);
56  }
57 
58  // void template<int N, int S>
59  // MixMaxEngine<N,S>::SetSeed64(uint64_t seed) {
60  // seed_spbox(fRngState, seed);
61  // iterate(fRngState);
62  // }
63 
64  // unsigned int template<int N, int S>
65  // MixMaxEngine<N,S>::GetSeed() const {
66  // return get_next(fRngState);
67  // }
68 
69  // generate one random number in interval ]0,1]
70  // apply skipping when needed
71 
72  template<int SkipNumber>
73  struct SkipFunction {
74  template<class Engine>
75  static void Apply (Engine * rng, int counter, int n) {
76  // apply skipping
77  if (counter < n) return;
78  for (int iskip = 0; iskip < SkipNumber; ++iskip)
79  rng->Iterate();
80  }
81  };
82  // specialization for SkipNumber = 0
83  template<>
84  struct SkipFunction<0> {
85  template<class Engine>
86  static void Apply (Engine *, int , int ) {
87  // no operation
88  }
89  };
90 
91  template<int N, int S>
92  double MixMaxEngine<N,S>::Rndm_impl() {
93  int counter = fRng->Counter();
94  SkipFunction<S>::Apply(fRng, counter, N);
95  //reset counter to original value
96  // otherwise we would skip -1 time
97  fRng->SetCounter(counter);
98  return fRng->Rndm();
99  }
100 
101  // generate one integer number
102  template<int N, int S>
103  uint64_t MixMaxEngine<N,S>::IntRndm() {
104  int counter = fRng->Counter();
105  SkipFunction<S>::Apply(fRng, counter,N);
106  fRng->SetCounter(counter);
107  return fRng->IntRndm();
108  }
109 
110  template<int N, int S>
111  uint64_t MixMaxEngine<N,S>::MaxInt() {
112  //return mixmax::mixmax_engine<N>::max();
113  return 2305843009213693951ULL;
114  }
115 
116  template<int N, int S>
117  uint64_t MixMaxEngine<N,S>::MinInt() {
118  //return mixmax::mixmax_engine<N>::min();
119  return 0;
120  }
121 
122  template<int N, int S>
123  void MixMaxEngine<N,S>::RndmArray(int n, double *array){
124  // Return an array of n random numbers uniformly distributed in ]0,1]
125  for (int i = 0; i < n; ++i)
126  array[i] = Rndm_impl();
127  }
128 
129  template<int N, int S>
130  void MixMaxEngine<N,S>::SetState(const std::vector<StateInt_t> & state) {
131  assert(state.size() >= N);
132  // for (int i = 0; i < N; ++i)
133  // fRng->S.V[i] = state[i];
134  // //set counter to fore iteration afterwards
135  // fRng->S.counter = N;
136  fRng->SetState(state);
137  fRng->SetCounter(N);
138  }
139 
140  template<int N, int S>
141  void MixMaxEngine<N,S>::GetState(std::vector<StateInt_t> & state) const {
142  state.resize(N);
143  fRng->GetState(state);
144  }
145 
146  template<int N, int S>
147  int MixMaxEngine<N,S>::Size() {
148  return MixMaxEngineImpl<N>::Size();
149  }
150 
151  template<int N, int S>
152  int MixMaxEngine<N,S>::Counter() const {
153  return fRng->Counter();
154  }
155 
156  template<int N, int S>
157  const char *MixMaxEngine<N, S>::Name() {
158  static const std::string name = "MixMax" + Util::ToString(N) + (S > 0 ? "_" + Util::ToString(S) : "");
159  return name.c_str();
160  }
161 
162  } // namespace Math
163 } // namespace ROOT
164 
165 #endif