MIRA
Random.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 
47 #ifndef _MIRA_RANDOM_H_
48 #define _MIRA_RANDOM_H_
49 
50 #include <stdlib.h>
51 
52 #include <utils/Singleton.h>
53 #include <utils/Time.h>
54 
55 #define MIRA_RANDOM mira::RandomGeneratorSingleton::instance()
56 
57 namespace mira
58 {
59 
61 
81 class RandomGeneratorSingleton : public LazySingleton<RandomGeneratorSingleton>
82 {
83 private:
86  seed();
87  }
88 
89 public:
100  void seed(uint64 seed = 0)
101  {
102  if (seed == 0)
103  srand48(Time::now().toUnixTimestamp());
104  else
105  srand48(seed);
106  }
107 
112  double random()
113  {
114  return drand48();
115  }
116 
121  template<typename T>
122  T uniform(T min, T max)
123  {
124  return min + (T)(random()*(max-min));
125  }
126 
132  double normal(double sigma = 1.0)
133  {
134  assert(sigma >= 0.0);
135  if (sigma == 0.0)
136  return 0.0;
137  double x, y, r2;
138  do {
139  // choose x,y in uniform square [-1,-1) to [+1,+1)
140  x = uniform(-1.0, 1.0);
141  y = uniform(-1.0, 1.0);
142 
143  // see if it is in the unit circle
144  r2 = x*x + y*y;
145  } while ((r2 > 1.0) || (r2 == 0));
146 
147  // Marsaglia polar method
148  return sigma * y * sqrt(-2.0*log(r2)/r2);
149  }
150 
152  double gaussian(double sigma = 1.0) {
153  return normal(sigma);
154  }
155 
156 };
157 
159 
160 }
161 
162 #endif
Singleton class for generating random numbers.
Definition: Random.h:81
uint64_t uint64
Definition: Types.h:65
double random()
Platform independent generation of random, double precision floating point random numbers in the inte...
Definition: Random.h:112
Implementation of the CreationPolicy that is used by the Singleton template.
Definition: Singleton.h:149
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
Time and Duration wrapper class.
double gaussian(double sigma=1.0)
deprecated, use normal() instead.
Definition: Random.h:152
Provided for convenience.
Definition: Singleton.h:528
T uniform(T min, T max)
Generate uniform distributed random numbers in the interval [min, max)
Definition: Random.h:122
A singleton class that can be freely configured using policies that control the creation, instantiation, lifetime and thread-safety.
static Time now()
Returns the current utc based time.
Definition: Time.h:481
void seed(uint64 seed=0)
Seed the random number generator.
Definition: Random.h:100
double normal(double sigma=1.0)
Generate standard normally distributed random numbers using a source of uniformly distributed random ...
Definition: Random.h:132