Bridge++  Version 1.5.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
randomNumberManager.cpp
Go to the documentation of this file.
1 
14 #include "randomNumberManager.h"
15 
16 #include "randomNumbers_Mseries.h"
17 #include "randomNumbers_MT19937.h"
18 #include "randomNumbers_SFMT.h"
19 
20 const std::string RandomNumberManager::class_name = "RandomNumberManager";
21 
22 //================================================================
24 
25 //================================================================
26 RandomNumbers *RandomNumberManager::factory(const std::string& rng_type, unsigned long seed)
27 {
28  if ((rng_type == "Mseries") || (rng_type == "mseries")) {
29  return new RandomNumbers_Mseries(seed);
30  } else if ((rng_type == "MT19937") || (rng_type == "mt19937")) {
31  return new RandomNumbers_MT19937(seed);
32 
33 #ifdef USE_SFMTLIB
34  } else if (rng_type == "SFMT") {
35  return new RandomNumbers_SFMT(seed);
36 #endif
37  } else {
38  vout.crucial("%s: unsupported random number generator: %s\n", class_name.c_str(), rng_type.c_str());
39  return NULL;
40  }
41 }
42 
43 
44 //================================================================
46 {
47  if (!s_rand) {
48  vout.crucial("Error: %s: uninitialized.\n", class_name.c_str());
49  exit(EXIT_FAILURE);
50  }
51 
52  return s_rand;
53 }
54 
55 
56 //================================================================
57 bool RandomNumberManager::initialize(const std::string& rng_type, unsigned long seed)
58 {
59  vout.detailed("%s: initialize: type = \"%s\", seed = %lu\n", class_name.c_str(), rng_type.c_str(), seed);
60 
61  if (s_rand) {
62  vout.crucial("Error: %s: already initialized.\n", class_name.c_str());
63 
64  exit(EXIT_FAILURE);
65  return false;
66  }
67 
68  s_rand = factory(rng_type, seed);
69 
70  if (!s_rand) {
71  vout.crucial("ERROR: %s: initialize failed.\n", class_name.c_str());
72  exit(EXIT_FAILURE);
73  }
74 
75  return s_rand != NULL;
76 }
77 
78 
79 //================================================================
81 {
82  if (s_rand) {
83  delete s_rand;
84  s_rand = NULL;
85  }
86 }
87 
88 
89 //================================================================
90 void RandomNumberManager::reset(unsigned long seed)
91 {
92  if (!s_rand) {
93  vout.crucial("Warning: %s: uninitialized.\n", class_name.c_str());
94  }
95 
96  if (s_rand) {
97  s_rand->reset(seed);
98  }
99 }
100 
101 
102 //================================================================
103 void RandomNumberManager::restore_state(const std::string& filename)
104 {
105  if (!s_rand) {
106  vout.crucial("Warning: %s: uninitialized.\n", class_name.c_str());
107  }
108 
109  if (s_rand) {
110  s_rand->read_file(filename);
111  }
112 }
113 
114 
115 //================================================================
116 void RandomNumberManager::save_state(const std::string& filename)
117 {
118  if (!s_rand) {
119  vout.crucial("Warning: %s: uninitialized.\n", class_name.c_str());
120  }
121 
122  if (s_rand) {
123  s_rand->write_file(filename);
124  }
125 }
126 
127 
128 //================================================================
129 RandomNumbers *RandomNumberManager::New(const std::string& rng_type, unsigned long seed)
130 {
131  return factory(rng_type, seed);
132 }
133 
134 
135 //================================================================
136 //================================================================
Random number generator base on M-series.
BridgeIO vout
Definition: bridgeIO.cpp:503
void detailed(const char *format,...)
Definition: bridgeIO.cpp:216
static RandomNumbers * getInstance()
static const std::string class_name
virtual void write_file(const std::string &)=0
static void save_state(const std::string &filename)
static bool initialize(const std::string &rng_type, unsigned long seed)
static RandomNumbers * factory(const std::string &rng_type, unsigned long seed)
static void restore_state(const std::string &filename)
virtual void reset(unsigned long seed)=0
reset state with new seed.
void crucial(const char *format,...)
Definition: bridgeIO.cpp:178
virtual void read_file(const std::string &)=0
save and load random number status.
static RandomNumbers * s_rand
static void reset(unsigned long seed)
Base class of random number generators.
Definition: randomNumbers.h:43
static RandomNumbers * New(const std::string &rng_type, unsigned long seed)