ARGoS  3
A parallel, multi-engine simulator for swarm robotics
core/utility/math/rng.h
Go to the documentation of this file.
00001 
00009 #ifndef ARGOS_RANDOM
00010 #define ARGOS_RANDOM
00011 
00012 namespace argos {
00013    class CRandom;
00014 }
00015 
00016 #include <argos3/core/utility/math/angles.h>
00017 #include <argos3/core/utility/math/range.h>
00018 
00019 #include <map>
00020 
00021 #ifdef ARGOS_WITH_GSL
00022 #   include <gsl/gsl_rng.h>
00023 #else
00024 #   include <cstdlib>
00025 #endif
00026 
00027 namespace argos {
00028 
00088    class CRandom {
00089 
00090    public:
00091 
00097       class CRNG {
00098 
00099       public:
00100 
00107          CRNG(UInt32 un_seed,
00108               const std::string& str_type = "mt19937");
00109 
00114          CRNG(const CRNG& c_rng);
00115 
00119          virtual ~CRNG();
00120 
00125          inline UInt32 GetSeed() const throw() {
00126             return m_unSeed;
00127          }
00128 
00135          inline void SetSeed(UInt32 un_seed) throw() {
00136             m_unSeed = un_seed;
00137          }
00138 
00143          inline std::string GetType() const throw() {
00144             return m_strType;
00145          }
00146 
00151          inline void SetType(const std::string& str_type) {
00152             m_strType = str_type;
00153          }
00154 
00159          void Reset();
00160 
00166          bool Bernoulli(Real f_true = 0.5);
00172          CRadians Uniform(const CRange<CRadians>& c_range);
00178          Real Uniform(const CRange<Real>& c_range);
00184          SInt32 Uniform(const CRange<SInt32>& c_range);
00190          UInt32 Uniform(const CRange<UInt32>& c_range);
00196          Real Exponential(Real f_mean);
00203          Real Gaussian(Real f_std_dev, Real f_mean = 0.0f);
00209          Real Rayleigh(Real f_sigma);
00210          
00217          Real Lognormal(Real f_sigma, Real f_mu);
00218 
00219       private:
00220 
00221          void CreateRNG();
00222          void DisposeRNG();
00223 
00224       private:
00225 
00226          UInt32 m_unSeed;
00227          std::string m_strType;
00228 #ifdef ARGOS_WITH_GSL
00229          gsl_rng* m_ptRNG;
00230 #else
00231          random_data* m_ptRNG;
00232          char* m_pchRNGState;
00233 #endif
00234          CRange<UInt32>* m_pcIntegerRNGRange;
00235 
00236       };
00237 
00242       class CCategory {
00243 
00244       public:
00245 
00251          CCategory(const std::string& str_id,
00252                    UInt32 un_seed);
00253 
00257          virtual ~CCategory();
00258 
00263          inline const std::string& GetId() const throw() {
00264             return m_strId;
00265          }
00270          void SetId(const std::string& str_id) {
00271             m_strId = str_id;
00272          }
00273 
00278          inline UInt32 GetSeed() const {
00279             return m_unSeed;
00280          }
00287          void SetSeed(UInt32 un_seed);
00288 
00294          CRNG* CreateRNG(const std::string& str_type = "mt19937");
00295 
00299          void ResetRNGs();
00300 
00306          void ReseedRNGs();
00307 
00308       private:
00309 
00310          std::string m_strId;
00311          std::vector<CRNG*> m_vecRNGList;
00312          UInt32 m_unSeed;
00313          CRNG m_cSeeder;
00314          CRange<UInt32> m_cSeedRange;
00315       };
00316 
00317    public:
00318 
00325       static bool CreateCategory(const std::string& str_category,
00326                                  UInt32 un_seed);
00332       static CCategory& GetCategory(const std::string& str_category);
00333 
00339       static bool ExistsCategory(const std::string& str_category);
00340 
00345       static void RemoveCategory(const std::string& str_category);
00346 
00353       static CRNG* CreateRNG(const std::string& str_category,
00354                              const std::string& str_type = "mt19937");
00355 
00361       static UInt32 GetSeedOf(const std::string& str_category);
00362 
00370       static void SetSeedOf(const std::string& str_category,
00371                             UInt32 un_seed);
00372 
00376       static void Reset();
00377 
00384 #ifdef ARGOS_WITH_GSL
00385       inline static const gsl_rng_type** GetRNGTypes() {
00386          return m_pptRNGTypes;
00387       }
00388 #endif
00389 
00390    private:
00391 
00392       static std::map<std::string, CCategory*> m_mapCategories;
00393 #ifdef ARGOS_WITH_GSL
00394       static const gsl_rng_type** m_pptRNGTypes;
00395 #endif
00396 
00397    };
00398 
00399 }
00400 
00401 #endif