rng.h
Go to the documentation of this file.
1 
9 #ifndef ARGOS_RANDOM
10 #define ARGOS_RANDOM
11 
12 namespace argos {
13  class CRandom;
14 }
15 
16 #include <argos3/core/utility/math/angles.h>
17 #include <argos3/core/utility/math/range.h>
18 #include <map>
19 
20 namespace argos {
21 
81  class CRandom {
82 
83  public:
84 
90  class CRNG {
91 
92  public:
93 
100  CRNG(UInt32 un_seed);
101 
106  CRNG(const CRNG& c_rng);
107 
111  virtual ~CRNG();
112 
117  inline UInt32 GetSeed() const throw() {
118  return m_unSeed;
119  }
120 
127  inline void SetSeed(UInt32 un_seed) throw() {
128  m_unSeed = un_seed;
129  }
130 
135  void Reset();
136 
142  bool Bernoulli(Real f_true = 0.5);
143 
149  CRadians Uniform(const CRange<CRadians>& c_range);
150 
156  Real Uniform(const CRange<Real>& c_range);
157 
163  SInt32 Uniform(const CRange<SInt32>& c_range);
164 
170  UInt32 Uniform(const CRange<UInt32>& c_range);
171 
177  Real Exponential(Real f_mean);
178 
184  UInt32 Poisson(Real f_mean);
185 
192  Real Gaussian(Real f_std_dev, Real f_mean = 0.0f);
193 
199  Real Rayleigh(Real f_sigma);
200 
207  Real Lognormal(Real f_sigma, Real f_mu);
208 
213  template <typename T>
214  void Shuffle(std::vector<T>& vec_data) {
215  UInt32 j;
216  for(UInt32 i = 0; i < vec_data.size()-1; ++i) {
217  j = i + Uniform(CRange<UInt32>(0, vec_data.size() - i));
218  T tTmp = vec_data[i];
219  vec_data[i] = vec_data[j];
220  vec_data[j] = tTmp;
221  }
222  }
223 
224  private:
225 
226  /*
227  * Generates a random 32bit unsigned integer.
228  * Used internally by all other functions.
229  */
230  UInt32 Uniform32bit();
231 
232  private:
233 
234  UInt32 m_unSeed;
235  UInt32* m_punState;
236  SInt32 m_nIndex;
237 
238  };
239 
244  class CCategory {
245 
246  public:
247 
253  CCategory(const std::string& str_id,
254  UInt32 un_seed);
255 
259  virtual ~CCategory();
260 
265  inline const std::string& GetId() const throw() {
266  return m_strId;
267  }
272  void SetId(const std::string& str_id) {
273  m_strId = str_id;
274  }
275 
280  inline UInt32 GetSeed() const {
281  return m_unSeed;
282  }
289  void SetSeed(UInt32 un_seed);
290 
295  CRNG* CreateRNG();
296 
300  void ResetRNGs();
301 
307  void ReseedRNGs();
308 
309  private:
310 
311  std::string m_strId;
312  std::vector<CRNG*> m_vecRNGList;
313  UInt32 m_unSeed;
314  CRNG m_cSeeder;
315  CRange<UInt32> m_cSeedRange;
316  };
317 
318  public:
319 
326  static bool CreateCategory(const std::string& str_category,
327  UInt32 un_seed);
333  static CCategory& GetCategory(const std::string& str_category);
334 
340  static bool ExistsCategory(const std::string& str_category);
341 
346  static void RemoveCategory(const std::string& str_category);
347 
353  static CRNG* CreateRNG(const std::string& str_category);
354 
360  static UInt32 GetSeedOf(const std::string& str_category);
361 
369  static void SetSeedOf(const std::string& str_category,
370  UInt32 un_seed);
371 
375  static void Reset();
376 
377  private:
378 
379  static std::map<std::string, CCategory*> m_mapCategories;
380  };
381 
382 }
383 
384 #endif
signed int SInt32
32-bit signed integer.
Definition: datatypes.h:93
unsigned int UInt32
32-bit unsigned integer.
Definition: datatypes.h:97
float Real
Collects all ARGoS code.
Definition: datatypes.h:39
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
It defines the basic type CRadians, used to store an angle value in radians.
Definition: angles.h:42
The ARGoS random number generator.
Definition: rng.h:81
static UInt32 GetSeedOf(const std::string &str_category)
Returns the seed of the wanted category.
Definition: rng.cpp:355
static void Reset()
Resets all the RNG categories.
Definition: rng.cpp:372
static CRNG * CreateRNG(const std::string &str_category)
Creates a new RNG inside the given category.
Definition: rng.cpp:347
static void RemoveCategory(const std::string &str_category)
Removes the wanted category.
Definition: rng.cpp:338
static void SetSeedOf(const std::string &str_category, UInt32 un_seed)
Sets the new seed of the wanted category.
Definition: rng.cpp:363
static bool CreateCategory(const std::string &str_category, UInt32 un_seed)
Creates a new category.
Definition: rng.cpp:298
static CCategory & GetCategory(const std::string &str_category)
Returns a reference to the wanted category.
Definition: rng.cpp:317
static bool ExistsCategory(const std::string &str_category)
Returns true if the given category exists in the pool.
Definition: rng.cpp:325
The RNG.
Definition: rng.h:90
void Shuffle(std::vector< T > &vec_data)
Shuffles the values of the given vector in-place.
Definition: rng.h:214
UInt32 Poisson(Real f_mean)
Returns a random value from a Poisson distribution.
Definition: rng.cpp:131
virtual ~CRNG()
Class destructor.
Definition: rng.cpp:61
bool Bernoulli(Real f_true=0.5)
Returns a random value from a Bernoulli distribution.
Definition: rng.cpp:80
UInt32 GetSeed() const
Returns the seed of this RNG.
Definition: rng.h:117
CRNG(UInt32 un_seed)
Class constructor.
Definition: rng.cpp:41
void SetSeed(UInt32 un_seed)
Sets the seed of this RNG.
Definition: rng.h:127
void Reset()
Reset the RNG.
Definition: rng.cpp:68
Real Lognormal(Real f_sigma, Real f_mu)
Returns a random value from a Lognormal distribution.
Definition: rng.cpp:188
Real Rayleigh(Real f_sigma)
Returns a random value from a Rayleigh distribution.
Definition: rng.cpp:171
Real Exponential(Real f_mean)
Returns a random value from an exponential distribution.
Definition: rng.cpp:123
Real Gaussian(Real f_std_dev, Real f_mean=0.0f)
Returns a random value from a Gaussian distribution.
Definition: rng.cpp:152
CRadians Uniform(const CRange< CRadians > &c_range)
Returns a random value from a uniform distribution.
Definition: rng.cpp:87
The RNG category.
Definition: rng.h:244
void ResetRNGs()
Resets the RNGs in this category.
Definition: rng.cpp:275
CCategory(const std::string &str_id, UInt32 un_seed)
Class constructor.
Definition: rng.cpp:236
const std::string & GetId() const
Returns the id of the category.
Definition: rng.h:265
CRNG * CreateRNG()
Creates a new RNG inside this category.
Definition: rng.cpp:264
virtual ~CCategory()
Class destructor.
Definition: rng.cpp:246
void SetSeed(UInt32 un_seed)
Sets the new seed of the category.
Definition: rng.cpp:256
void ReseedRNGs()
Sets new seed for the RNGs in this category.
Definition: rng.cpp:288
UInt32 GetSeed() const
Returns the seed of the category.
Definition: rng.h:280
void SetId(const std::string &str_id)
Sets the new id of the category.
Definition: rng.h:272