ssmkit  master-68aed98
generator.hpp
Go to the documentation of this file.
1 
7 #ifndef SSMPACK_RANDOM_GENERATOR_HPP
8 #define SSMPACK_RANDOM_GENERATOR_HPP
9 
10 #include <random>
11 
12 namespace ssmkit {
13 namespace random {
14 
15 typedef std::mt19937 CoreGenerator;
16 
20 class Generator {
21  private:
23  CoreGenerator gen_;
31  Generator() {
32  // random initialization on construction
33  setRandomSeed();
34  }
35 
36  public:
38  static Generator &get() {
39  // this is thread local to avoid race condition on gen_
40  static thread_local Generator instance;
41  return instance;
42  }
44  CoreGenerator & getGenerator() {return gen_;}
46  template<class TSeed>
47  void setSeed(TSeed seed) {gen_.seed(seed);}
50  {
51  std::random_device rd;
52  gen_.seed(rd());
53  }
54 
55 };
56 
58 inline void setRandomSeed()
59 {
61 }
62 
64 template<class TSeed>
65 void setSeed(TSeed seed)
66 {
67  Generator::get().setSeed(seed);
68 }
69 
70 } // namespace random
71 } // namespace ssmkit
72 
73 
74 #endif //SSMPACK_RANDOM_GENERATOR_HPP
static Generator & get()
Returns a reference to singleton instance.
Definition: generator.hpp:38
A singleton wrapper of an instance of random generator.
Definition: generator.hpp:20
void setSeed(TSeed seed)
Sets the seed for the core generator.
Definition: generator.hpp:47
std::mt19937 CoreGenerator
Definition: generator.hpp:15
void setRandomSeed()
Convenient function to set random seed for singleton generator object.
Definition: generator.hpp:58
void setSeed(TSeed seed)
Convenient function to set seed for singleton generator object.
Definition: generator.hpp:65
CoreGenerator & getGenerator()
Returns reference to the core generator.
Definition: generator.hpp:44
void setRandomSeed()
Sets a random seed for core generator.
Definition: generator.hpp:49