10 #include <argos3/core/utility/configuration/argos_exception.h>
11 #include <argos3/core/utility/logging/argos_log.h>
22 static const SInt32 N = 624;
24 static const UInt32 MATRIX_A = 0x9908b0dfUL;
25 static const UInt32 UPPER_MASK = 0x80000000UL;
26 static const UInt32 LOWER_MASK = 0x7fffffffUL;
27 static const CRange<UInt32> INT_RANGE = CRange<UInt32>(0, 0xFFFFFFFFUL);
29 std::map<std::string, CRandom::CCategory*> CRandom::m_mapCategories;
32 #define CHECK_CATEGORY(category) \
33 std::map<std::string, CCategory*>::iterator itCategory = m_mapCategories.find(category); \
34 if(itCategory == m_mapCategories.end()) { \
35 THROW_ARGOSEXCEPTION("CRandom:: can't find category \"" << category << "\"."); \
52 m_unSeed(c_rng.m_unSeed),
54 m_nIndex(c_rng.m_nIndex) {
55 ::memcpy(m_punState, c_rng.m_punState, N *
sizeof(
UInt32));
69 m_punState[0]= m_unSeed & 0xffffffffUL;
70 for (m_nIndex = 1; m_nIndex < N; ++m_nIndex) {
71 m_punState[m_nIndex] =
72 (1812433253UL * (m_punState[m_nIndex-1] ^ (m_punState[m_nIndex-1] >> 30)) + m_nIndex);
73 m_punState[m_nIndex] &= 0xffffffffUL;
81 return Uniform32bit() < f_true * INT_RANGE.
GetMax();
125 return -
Log(Uniform(fRange)) * f_mean;
143 fP *= f_mean / unRetVal;
161 fNum1 = Uniform(fRange);
162 fNum2 = Uniform(fRange);
163 fSquare = fNum1 * fNum1 + fNum2 * fNum2;
164 }
while(fSquare >= 1);
165 return f_mean + f_std_dev * fNum1 *
Sqrt(-2.0f *
Log(fSquare) / fSquare);
176 fValue = Uniform(cUnitRange);
182 return f_sigma *
Sqrt(-2.0f *
Log(fValue));
191 fValue = Gaussian(1,0);
195 return std::exp(f_mu + f_sigma * fValue);
201 UInt32 CRandom::CRNG::Uniform32bit() {
203 static UInt32 mag01[2] = { 0x0UL, MATRIX_A };
208 for (kk = 0; kk < N -
M; ++kk) {
209 y = (m_punState[kk] & UPPER_MASK) | (m_punState[kk+1] & LOWER_MASK);
210 m_punState[kk] = m_punState[kk+
M] ^ (y >> 1) ^ mag01[y & 0x1UL];
212 for (; kk < N - 1; ++kk) {
213 y = (m_punState[kk] & UPPER_MASK) | (m_punState[kk+1] & LOWER_MASK);
214 m_punState[kk] = m_punState[kk+(
M-N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
216 y = (m_punState[N-1] & UPPER_MASK) | (m_punState[0] & LOWER_MASK);
217 m_punState[N-1] = m_punState[
M-1] ^ (y >> 1) ^ mag01[y & 0x1UL];
222 y = m_punState[m_nIndex++];
226 y ^= (y << 7) & 0x9d2c5680UL;
227 y ^= (y << 15) & 0xefc60000UL;
241 m_cSeedRange(1, std::numeric_limits<
UInt32>::max()) {}
247 while(! m_vecRNGList.empty()) {
248 delete m_vecRNGList.back();
249 m_vecRNGList.pop_back();
258 m_cSeeder.SetSeed(m_unSeed);
268 m_vecRNGList.push_back(
new CRNG(unSeed));
269 return m_vecRNGList.back();
280 for(
size_t i = 0; i < m_vecRNGList.size(); ++i) {
281 m_vecRNGList[i]->Reset();
289 for(
size_t i = 0; i < m_vecRNGList.size(); ++i) {
291 m_vecRNGList[i]->SetSeed(m_cSeeder.Uniform(m_cSeedRange));
301 auto itCategory = m_mapCategories.find(str_category);
302 if(itCategory == m_mapCategories.end()) {
304 m_mapCategories.insert(
305 std::pair<std::string,
319 return *(itCategory->second);
340 delete itCategory->second;
341 m_mapCategories.erase(itCategory);
349 return itCategory->second->CreateRNG();
357 return itCategory->second->GetSeed();
366 itCategory->second->SetSeed(un_seed);
373 for(
auto itCategory = m_mapCategories.begin();
374 itCategory != m_mapCategories.end();
376 itCategory->second->ResetRNGs();
signed int SInt32
32-bit signed integer.
unsigned int UInt32
32-bit unsigned integer.
float Real
Collects all ARGoS code.
#define CHECK_CATEGORY(category)
The namespace containing all the ARGoS related code.
The exception that wraps all errors in ARGoS.
It defines the basic type CRadians, used to store an angle value in radians.
bool WithinMinBoundExcludedMaxBoundExcluded(const T &t_value) const
void MapValueIntoRange(U &t_output_value, const T &t_input_value, const CRange< U > &c_range) const
static UInt32 GetSeedOf(const std::string &str_category)
Returns the seed of the wanted category.
static void Reset()
Resets all the RNG categories.
static CRNG * CreateRNG(const std::string &str_category)
Creates a new RNG inside the given category.
static void RemoveCategory(const std::string &str_category)
Removes the wanted category.
static void SetSeedOf(const std::string &str_category, UInt32 un_seed)
Sets the new seed of the wanted category.
static bool CreateCategory(const std::string &str_category, UInt32 un_seed)
Creates a new category.
static CCategory & GetCategory(const std::string &str_category)
Returns a reference to the wanted category.
static bool ExistsCategory(const std::string &str_category)
Returns true if the given category exists in the pool.
UInt32 Poisson(Real f_mean)
Returns a random value from a Poisson distribution.
virtual ~CRNG()
Class destructor.
bool Bernoulli(Real f_true=0.5)
Returns a random value from a Bernoulli distribution.
CRNG(UInt32 un_seed)
Class constructor.
void Reset()
Reset the RNG.
Real Lognormal(Real f_sigma, Real f_mu)
Returns a random value from a Lognormal distribution.
Real Rayleigh(Real f_sigma)
Returns a random value from a Rayleigh distribution.
Real Exponential(Real f_mean)
Returns a random value from an exponential distribution.
Real Gaussian(Real f_std_dev, Real f_mean=0.0f)
Returns a random value from a Gaussian distribution.
CRadians Uniform(const CRange< CRadians > &c_range)
Returns a random value from a uniform distribution.
void ResetRNGs()
Resets the RNGs in this category.
CCategory(const std::string &str_id, UInt32 un_seed)
Class constructor.
CRNG * CreateRNG()
Creates a new RNG inside this category.
virtual ~CCategory()
Class destructor.
void SetSeed(UInt32 un_seed)
Sets the new seed of the category.
void ReseedRNGs()
Sets new seed for the RNGs in this category.