random_number_generator.hpp
Go to the documentation of this file.
1 
8 /*****************************************************************************
9 ** Ifdefs
10 *****************************************************************************/
11 
12 #ifndef ECL_TIME_RANDOM_NUMBER_GENERATOR_HPP_
13 #define ECL_TIME_RANDOM_NUMBER_GENERATOR_HPP_
14 
15 /*****************************************************************************
16 ** Includes
17 *****************************************************************************/
18 
19 #include <cmath>
20 #include <ctime>
21 #include <cstdlib>
22 #include <ecl/config.hpp>
23 
24 #if defined(ECL_IS_WIN32)
25  getpid = _getpid
26 #endif
27 
28 /*****************************************************************************
29 ** Namespaces
30 *****************************************************************************/
31 
32 namespace ecl {
33 
34 /*****************************************************************************
35 ** Random Number Generator
36 *****************************************************************************/
40 template<typename T>
41 class RandomNumberGenerator
42 {
43 public:
44  RandomNumberGenerator( const unsigned int randSeed )
45  {
46  srand( randSeed );
47  }
48 
50  {
52  }
53 
54  T uniform( const T & range)
55  {
56  return (uniform()-0.5)*2.0*range;
57  }
58 
59  T uniform()
60  {
61  return static_cast<T>(rand())/static_cast<T>(RAND_MAX);
62  }
63 
64  T gaussian( const T & std, const T & mu = 0 )
65  {
66  T x1, x2, w, y1;
67  static T y2;
68  static bool use_last = false;
69 
70  if (use_last) /* use value from previous call */
71  {
72  y1 = y2;
73  use_last = false;
74  }
75  else
76  {
77  do {
78  x1 = 2.0 * uniform() - 1.0;
79  x2 = 2.0 * uniform() - 1.0;
80  w = x1 * x1 + x2 * x2;
81  } while ( w >= 1.0 );
82 
83  w = std::sqrt( (-2.0 * std::log( w ) ) / w );
84  y1 = x1 * w;
85  y2 = x2 * w;
86  use_last = true;
87  }
88 
89  return mu + y1 * std;
90  }
91 
92 private:
111  static void seed()
112  {
113  unsigned long a = clock();
114  unsigned long b = ::time(NULL);
115  unsigned long c = getpid();
116  a=a-b; a=a-c; a=a^(c >> 13);
117  b=b-c; b=b-a; b=b^(a << 8);
118  c=c-a; c=c-b; c=c^(b >> 13);
119  a=a-b; a=a-c; a=a^(c >> 12);
120  b=b-c; b=b-a; b=b^(a << 16);
121  c=c-a; c=c-b; c=c^(b >> 5);
122  a=a-b; a=a-c; a=a^(c >> 3);
123  b=b-c; b=b-a; b=b^(a << 10);
124  c=c-a; c=c-b; c=c^(b >> 15);
125  srand(c);
126  }
127 };
128 
129 } // namespace ecl
130 
131 
132 #endif /* ECL_TIME_RANDOM_NUMBER_GENERATOR_HPP_ */
ecl::RandomNumberGenerator::RandomNumberGenerator
RandomNumberGenerator()
Definition: random_number_generator.hpp:63
ecl::RandomNumberGenerator::uniform
T uniform()
Definition: random_number_generator.hpp:73
config.hpp
ecl::RandomNumberGenerator::seed
static void seed()
Seed the system random number generator.
Definition: random_number_generator.hpp:125
ecl::RandomNumberGenerator::gaussian
T gaussian(const T &std, const T &mu=0)
Definition: random_number_generator.hpp:78
ecl
Embedded control libraries.


ecl_time
Author(s): Daniel Stonier
autogenerated on Wed Mar 2 2022 00:16:19