Pallas Solver  0.1
C++ Global Optimization Algorithms
random_number_generator.h
Go to the documentation of this file.
1 
10 // Pallas Solver
11 // Copyright 2015. All rights reserved.
12 //
13 // Redistribution and use in source and binary forms, with or without
14 // modification, are permitted provided that the following conditions are met:
15 //
16 // * Redistributions of source code must retain the above copyright notice,
17 // this list of conditions and the following disclaimer.
18 // * Redistributions in binary form must reproduce the above copyright notice,
19 // this list of conditions and the following disclaimer in the documentation
20 // and/or other materials provided with the distribution.
21 //
22 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 // POSSIBILITY OF SUCH DAMAGE.
33 //
34 // Author: ryan.latture@gmail.com (Ryan Latture)
35 
36 #ifndef PALLAS_RANDOM_NUMBER_GENERATOR_H
37 #define PALLAS_RANDOM_NUMBER_GENERATOR_H
38 
39 #include <random>
40 #include <type_traits>
41 
42 namespace pallas {
43  namespace internal {
49  template <class T, class Enable = void> class RandomNumberGenerator;
50 
51  template<class T>
52  class RandomNumberGenerator<T, typename std::enable_if<std::is_integral<T>::value >::type> {
53  typedef std::mt19937 Engine;
54  typedef std::uniform_int_distribution<T> Distribution;
55 
56  private:
57  Engine *_eng;
58  Distribution *_dist;
59  std::random_device rd;
61  public:
69  RandomNumberGenerator(T minVal, T maxVal) {
70  std::random_device rd;
71  _eng = new Engine(rd());
72  _dist = new Distribution(minVal, maxVal);
73  };
74 
85  RandomNumberGenerator(T minVal, T maxVal, int seed) {
86  std::random_device rd;
87  _eng = new Engine(seed);
88  _dist = new Distribution(minVal, maxVal);
89  };
90 
96  std::random_device rd;
97  _eng = new Engine(rd());
98  _dist = new Distribution(0, 1);
99  };
100 
110  std::random_device rd;
111  _eng = new Engine(seed);
112  _dist = new Distribution(0, 1);
113  };
114 
116  delete _dist;
117  delete _eng;
118  };
119 
125  inline T operator()() {
126  return (*_dist)(*_eng);
127  };
128  };
129 
130  template<class T>
131  class RandomNumberGenerator<T, typename std::enable_if<std::is_floating_point<T>::value >::type> {
132  typedef std::mt19937 Engine;
133  typedef std::uniform_real_distribution<T> Distribution;
134 
135  private:
136  Engine *_eng;
137  Distribution *_dist;
138  std::random_device rd;
140  public:
148  RandomNumberGenerator(T minVal, T maxVal) {
149  std::random_device rd;
150  _eng = new Engine(rd());
151  _dist = new Distribution(minVal, maxVal);
152  };
153 
164  RandomNumberGenerator(T minVal, T maxVal, int seed) {
165  std::random_device rd;
166  _eng = new Engine(seed);
167  _dist = new Distribution(minVal, maxVal);
168  };
169 
175  std::random_device rd;
176  _eng = new Engine(rd());
177  _dist = new Distribution(0, 1);
178  };
179 
189  std::random_device rd;
190  _eng = new Engine(seed);
191  _dist = new Distribution(0, 1);
192  };
193 
195  delete _dist;
196  delete _eng;
197  };
198 
204  inline T operator()() {
205  return (*_dist)(*_eng);
206  };
207  };
208 
209  } // namespace internal
210 } // namespace pallas
211 
212 #endif //PALLAS_RANDOM_NUMBER_GENERATOR_H
T operator()()
Returns a random number.
Definition: random_number_generator.h:125
RandomNumberGenerator(T minVal, T maxVal)
Constructor with min and max values specified.
Definition: random_number_generator.h:148
RandomNumberGenerator(T minVal, T maxVal, int seed)
Constructor with min and max values specified and explicit integer seed.
Definition: random_number_generator.h:164
Definition: basinhopping.h:51
RandomNumberGenerator(T minVal, T maxVal, int seed)
Constructor with min and max values specified and explicit integer seed.
Definition: random_number_generator.h:85
RandomNumberGenerator(T minVal, T maxVal)
Constructor with min and max values specified.
Definition: random_number_generator.h:69
Generates random numbers.
Definition: random_number_generator.h:49
RandomNumberGenerator(int seed)
Constructor with default range &#39;[0, 1)&#39; and explicit integer seed.
Definition: random_number_generator.h:188
RandomNumberGenerator(int seed)
Constructor with default range &#39;[0, 1)&#39; and explicit integer seed.
Definition: random_number_generator.h:109