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;
140 fNum1 = Uniform(fRange);
141 fNum2 = Uniform(fRange);
142 fSquare = fNum1 * fNum1 + fNum2 * fNum2;
143 }
while(fSquare >= 1);
144 return f_mean + f_std_dev * fNum1 *
Sqrt(-2.0f *
Log(fSquare) / fSquare);
155 fValue = Uniform(cUnitRange);
161 return f_sigma *
Sqrt(-2.0f *
Log(fValue));
170 fValue = Gaussian(1,0);
174 return std::exp(f_mu + f_sigma * fValue);
180 UInt32 CRandom::CRNG::Uniform32bit() {
182 static UInt32 mag01[2] = { 0x0UL, MATRIX_A };
187 for (kk = 0; kk < N -
M; ++kk) {
188 y = (m_punState[kk] & UPPER_MASK) | (m_punState[kk+1] & LOWER_MASK);
189 m_punState[kk] = m_punState[kk+
M] ^ (y >> 1) ^ mag01[y & 0x1UL];
191 for (; kk < N - 1; ++kk) {
192 y = (m_punState[kk] & UPPER_MASK) | (m_punState[kk+1] & LOWER_MASK);
193 m_punState[kk] = m_punState[kk+(
M-N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
195 y = (m_punState[N-1] & UPPER_MASK) | (m_punState[0] & LOWER_MASK);
196 m_punState[N-1] = m_punState[
M-1] ^ (y >> 1) ^ mag01[y & 0x1UL];
201 y = m_punState[m_nIndex++];
205 y ^= (y << 7) & 0x9d2c5680UL;
206 y ^= (y << 15) & 0xefc60000UL;
220 m_cSeedRange(1, std::numeric_limits<
UInt32>::max()) {}
226 while(! m_vecRNGList.empty()) {
227 delete m_vecRNGList.back();
228 m_vecRNGList.pop_back();
237 m_cSeeder.SetSeed(m_unSeed);
247 m_vecRNGList.push_back(
new CRNG(unSeed));
248 return m_vecRNGList.back();
259 for(
size_t i = 0; i < m_vecRNGList.size(); ++i) {
260 m_vecRNGList[i]->Reset();
268 for(
size_t i = 0; i < m_vecRNGList.size(); ++i) {
270 m_vecRNGList[i]->SetSeed(m_cSeeder.Uniform(m_cSeedRange));
280 std::map<std::string, CCategory*>::iterator itCategory = m_mapCategories.find(str_category);
281 if(itCategory == m_mapCategories.end()) {
283 m_mapCategories.insert(
284 std::pair<std::string,
298 return *(itCategory->second);
319 delete itCategory->second;
320 m_mapCategories.erase(itCategory);
328 return itCategory->second->CreateRNG();
336 return itCategory->second->GetSeed();
345 itCategory->second->SetSeed(un_seed);
352 for(std::map<std::string, CCategory*>::iterator itCategory = m_mapCategories.begin();
353 itCategory != m_mapCategories.end();
355 itCategory->second->ResetRNGs();