MIRA
RandomGenerator.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
48 #ifndef _MIRA_RANDOMGENERATOR_H_
49 #define _MIRA_RANDOMGENERATOR_H_
50 
51 #include <limits>
52 
53 #ifndef Q_MOC_RUN
54 #include <boost/random/mersenne_twister.hpp>
55 #include <boost/random/variate_generator.hpp>
56 #include <boost/random/uniform_real.hpp>
57 #include <boost/random/uniform_int.hpp>
58 #endif
59 
60 #include <platform/Types.h>
61 #include <utils/Time.h>
62 
63 namespace mira {
64 
66 
95 template <typename Distribution, typename Engine=boost::mt19937>
97 {
98 public:
99  typedef typename Distribution::result_type result_type;
100 
101 public:
102 
111  mGenerator(mEngine, Distribution()) {
112  }
113 
121  RandomGenerator(const Distribution& dist) :
122  mGenerator(mEngine, dist) {
123  }
124 
134  mGenerator(mEngine, other.distribution()) {
135  }
136 
137 public:
138 
143  RandomGenerator& operator=(const Distribution& dist) {
144  mGenerator.distribution() = dist;
145  return *this;
146  }
147 
148 
157  mGenerator.distribution() = other.distribution();
158  return *this;
159  }
160 
161 
162 public:
163 
168  return mGenerator();
169  }
170 
171 public:
172 
178  void seed() {
179  seed((uint32)(Time::now()-Time::unixEpoch()).total_nanoseconds());
180  }
181 
187  void seed(uint32 value) {
188  mEngine.seed(value);
189  }
190 
191 public:
192 
196  Distribution* operator->() {
197  return &mGenerator.distribution();
198  }
199 
203  const Distribution* operator->() const {
204  return &mGenerator.distribution();
205  }
206 
210  Distribution& distribution() {
211  return mGenerator.distribution();
212  }
213 
217  const Distribution& distribution() const {
218  return mGenerator.distribution();
219  }
220 
221 public:
222 
226  Engine& engine() {
227  return mEngine;
228  }
229 
233  const Engine& engine() const {
234  return mEngine;
235  }
236 
237 private:
238 
239  Engine mEngine;
240  // we can remove the variate_generator for boost >= 1.47
241  boost::variate_generator<Engine&, Distribution> mGenerator;
242 };
243 
249 #define MIRA_RANDOM_GENERATOR_COMMON(Derived, TDistribution) \
250  typedef RandomGenerator<TDistribution> Base; \
251  typedef TDistribution Distribution; \
252  Derived(const Distribution& dist) : Base(dist) {} \
253  Derived(const Derived& other) : Base(other) {} \
254  Derived& operator=(const Distribution& dist) { \
255  Base::operator=(dist); \
256  return *this; \
257  } \
258  Derived& operator=(const Derived& other) { \
259  Base::operator=(other); \
260  return *this; \
261  }
262 
264 
265 } // namespace
266 
267 #endif
RandomGenerator & operator=(const Distribution &dist)
Assigns the given distribution (respectively its parameters) to this random generator.
Definition: RandomGenerator.h:143
Typedefs for OS independent basic data types.
RandomGenerator & operator=(const RandomGenerator &other)
Assignment operator that copies the distribution but not the random generator engine, since engines cannot be copied.
Definition: RandomGenerator.h:156
void seed()
Seeds the random generator using the current system time.
Definition: RandomGenerator.h:178
Engine & engine()
Provides direct access to the underlying random generator engine.
Definition: RandomGenerator.h:226
const Engine & engine() const
Provides direct access to the underlying random generator engine.
Definition: RandomGenerator.h:233
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
Distribution & distribution()
Provides direct access to the underlying random distribution.
Definition: RandomGenerator.h:210
Time and Duration wrapper class.
Template class to easily generate random generators using the boost::random distributions and generat...
Definition: RandomGenerator.h:96
static Time unixEpoch()
Returns the unix epoch 1.1.1970 0:0:0.000.
Definition: Time.h:514
RandomGenerator()
Generates the random generator with default parameters for the random distribution.
Definition: RandomGenerator.h:110
const Distribution & distribution() const
Provides direct access to the underlying random distribution.
Definition: RandomGenerator.h:217
Distribution * operator->()
Provides direct access to the underlying random distribution.
Definition: RandomGenerator.h:196
const Distribution * operator->() const
Provides direct access to the underlying random distribution.
Definition: RandomGenerator.h:203
RandomGenerator(const RandomGenerator &other)
Copy constructor that copies the distribution but initializes the random generator engine from the sc...
Definition: RandomGenerator.h:133
Distribution::result_type result_type
Definition: RandomGenerator.h:99
RandomGenerator(const Distribution &dist)
Generates the random generator with the parameters provided by the given distribution.
Definition: RandomGenerator.h:121
result_type operator()()
Draws a sample from the random distribution.
Definition: RandomGenerator.h:167
static Time now() static Time eternity()
Returns the current utc based time.
Definition: Time.h:484
void seed(uint32 value)
Seeds the random generator with the given seed.
Definition: RandomGenerator.h:187