Random.h
Go to the documentation of this file.
1 /*
2  * File: Random.h
3  * Project: DUtils library
4  * Author: Dorian Galvez-Lopez
5  * Date: April 2010, November 2011
6  * Description: manages pseudo-random numbers
7  * License: see the LICENSE.txt file
8  *
9  */
10 
11 #pragma once
12 #ifndef __D_RANDOM__
13 #define __D_RANDOM__
14 
15 #include <cstdlib>
16 #include <vector>
17 
18 namespace DUtils {
19 
21 class Random
22 {
23 public:
25 
26 public:
30  static void SeedRand();
31 
36  static void SeedRandOnce();
37 
42  static void SeedRand(int seed);
43 
49  static void SeedRandOnce(int seed);
50 
55  template <class T>
56  static T RandomValue(){
57  return (T)rand()/(T)RAND_MAX;
58  }
59 
66  template <class T>
67  static T RandomValue(T min, T max){
68  return Random::RandomValue<T>() * (max - min) + min;
69  }
70 
77  static int RandomInt(int min, int max);
78 
84  template <class T>
85  static T RandomGaussianValue(T mean, T sigma)
86  {
87  // Box-Muller transformation
88  T x1, x2, w, y1;
89 
90  do {
91  x1 = (T)2. * RandomValue<T>() - (T)1.;
92  x2 = (T)2. * RandomValue<T>() - (T)1.;
93  w = x1 * x1 + x2 * x2;
94  } while ( w >= (T)1. || w == (T)0. );
95 
96  w = sqrt( ((T)-2.0 * log( w ) ) / w );
97  y1 = x1 * w;
98 
99  return( mean + y1 * sigma );
100  }
101 
102 private:
103 
105  static bool m_already_seeded;
106 
107 };
108 
109 // ---------------------------------------------------------------------------
110 
113 {
114 public:
115 
121  UnrepeatedRandomizer(int min, int max);
123 
129 
134  UnrepeatedRandomizer& operator=(const UnrepeatedRandomizer& rnd);
135 
141  int get();
142 
149  inline bool empty() const { return m_values.empty(); }
150 
155  inline unsigned int left() const { return m_values.size(); }
156 
160  void reset();
161 
162 protected:
163 
167  void createValues();
168 
169 protected:
170 
172  int m_min;
174  int m_max;
175 
177  std::vector<int> m_values;
178 
179 };
180 
181 }
182 
183 #endif
184 
Functions to generate pseudo-random numbers.
Definition: Random.h:21
static void SeedRandOnce()
Definition: Random.cpp:24
Definition: Random.h:18
static T RandomValue(T min, T max)
Definition: Random.h:67
static T RandomValue()
Definition: Random.h:56
static bool m_already_seeded
If SeedRandOnce() or SeedRandOnce(int) have already been called.
Definition: Random.h:105
int m_max
Max of range of values.
Definition: Random.h:174
static T RandomGaussianValue(T mean, T sigma)
Definition: Random.h:85
static void SeedRand()
Definition: Random.cpp:18
static int RandomInt(int min, int max)
Definition: Random.cpp:47
TFSIMD_FORCE_INLINE const tfScalar & w() const
int min(int a, int b)
Provides pseudo-random numbers with no repetitions.
Definition: Random.h:112
unsigned int left() const
Definition: Random.h:155
int m_min
Min of range of values.
Definition: Random.h:172
std::vector< int > m_values
Available values.
Definition: Random.h:177


orb_slam2_ros
Author(s):
autogenerated on Wed Apr 21 2021 02:53:05