MachineIntelligenceCore:Algorithms
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
RandomGenerator.cpp
Go to the documentation of this file.
1 
24 
25 #include <logger/Log.hpp>
26 
27 namespace mic {
28 namespace utils {
29 
30 // Init generator instance - as NULL.
31 boost::atomic<RandomGenerator*> RandomGenerator::instance_(NULL);
32 
33 // Initilize mutex.
35 
36 
38  // Try to load the instance - first check.
39  RandomGenerator* tmp = instance_.load(boost::memory_order_consume);
40  // If instance does not exist.
41  if (!tmp) {
42  // Enter critical section.
43  boost::mutex::scoped_lock guard(instantiation_mutex);
44  // Try to load the instance - second check.
45  tmp = instance_.load(boost::memory_order_consume);
46  // If still does not exist - create new instance.
47  if (!tmp) {
48  tmp = new RandomGenerator;
49  instance_.store(tmp, boost::memory_order_release);
50  }//: if
51  // Exit critical section.
52  }//: if
53  // Return instance.
54  return tmp;
55 }
56 
57 
59  rng_mt19937_64(rd()),
60  uniform_int_dist(0, RAND_MAX),
61  uniform_real_dist(0, 1),
62  normal_real_dist(0, 1)
63 {
64  // TODO Auto-generated constructor stub
65 
66 }
67 
68 
69 uint64_t RandomGenerator::uniRandInt(int min, int max) {
70  LOG(LTRACE)<<"uniRandInt";
71  std::uniform_int_distribution<> dist(min, max);
72  return dist(rng_mt19937_64);
73  //return uniform_int_dist(rng_mt19937_64);
74 }
75 
76 double RandomGenerator::uniRandReal(double min, double max) {
77  LOG(LTRACE)<<"uniRandReal";
78  return (uniform_real_dist(rng_mt19937_64) + min)/(max - min);
79 }
80 
81 double RandomGenerator::normRandReal(double mean, double variance) {
82  LOG(LTRACE)<<"normRandReal";
83  return (normal_real_dist(rng_mt19937_64) * variance + mean);
84 }
85 
86 
87 } /* namespace utils */
88 } /* namespace mic */
double uniRandReal(double min=0, double max=1)
Contains declaration of a random generator singleton.
Random generator - defined in the form of a singleton, with double-checked locking pattern (DCLP) bas...
std::uniform_real_distribution uniform_real_dist
Uniform distribution from 0 to 1 (real values).
static boost::atomic< RandomGenerator * > instance_
uint64_t uniRandInt(int min=0, int max=RAND_MAX)
static RandomGenerator * getInstance()
static boost::mutex instantiation_mutex
double normRandReal(double mean=0, double variance=1)
std::normal_distribution normal_real_dist
Normal distribution from 0 to 1 (real values).