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 defined(MIRA_LINUX)
103  if (seed == 0)
104  srand48(Time::now().toUnixTimestamp());
105  else
106  srand48(seed);
107 #elif defined(MIRA_WINDOWS)
108  if (seed == 0)
109  srand((unsigned int)(Time::now().toUnixTimestamp()));
110  else
111  srand((unsigned int)seed);
112 #endif
113  }
114 
119  double random()
120  {
121 #if defined(MIRA_LINUX)
122  return drand48();
123 #elif defined(MIRA_WINDOWS)
124  return 1.0*rand()/RAND_MAX;
125 #endif
126  }
127 
132  template<typename T>
133  T uniform(T min, T max)
134  {
135  return min + (T)(random()*(max-min));
136  }
137 
143  double normal(double sigma = 1.0)
144  {
145  assert(sigma >= 0.0);
146  if (sigma == 0.0)
147  return 0.0;
148  double x, y, r2;
149  do {
150  // choose x,y in uniform square [-1,-1) to [+1,+1)
151  x = uniform(-1.0, 1.0);
152  y = uniform(-1.0, 1.0);
153 
154  // see if it is in the unit circle
155  r2 = x*x + y*y;
156  } while ((r2 > 1.0) || (r2 == 0));
157 
158  // Marsaglia polar method
159  return sigma * y * sqrt(-2.0*log(r2)/r2);
160  }
161 
163  double gaussian(double sigma = 1.0) {
164  return normal(sigma);
165  }
166 
167 };
168 
170 
171 }
172 
173 #endif
Singleton class for generating random numbers.
Definition: Random.h:81
double random()
Platform independent generation of random, double precision floating point random numbers in the inte...
Definition: Random.h:119
Implementation of the CreationPolicy that is used by the Singleton template.
Definition: Singleton.h:174
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:163
Provided for convenience.
Definition: Singleton.h:564
T uniform(T min, T max)
Generate uniform distributed random numbers in the interval [min, max)
Definition: Random.h:133
A singleton class that can be freely configured using policies that control the creation, instantiation, lifetime and thread-safety.
void seed(uint64 seed=0)
Seed the random number generator.
Definition: Random.h:100
static Time now() static Time eternity()
Returns the current utc based time.
Definition: Time.h:479
double normal(double sigma=1.0)
Generate standard normally distributed random numbers using a source of uniformly distributed random ...
Definition: Random.h:143