Calc: Make std::default_random_engine static

This commit is contained in:
Birunthan Mohanathas 2013-12-08 13:40:48 +02:00
parent 3b1dfbac7e
commit 3383713853
2 changed files with 11 additions and 15 deletions

View File

@ -24,6 +24,9 @@
const int DEFAULT_LOWER_BOUND = 0; const int DEFAULT_LOWER_BOUND = 0;
const int DEFAULT_UPPER_BOUND = 100; const int DEFAULT_UPPER_BOUND = 100;
std::default_random_engine MeasureCalc::c_RandomEngine =
std::default_random_engine(std::random_device()());
/* /*
** The constructor ** The constructor
** **
@ -33,12 +36,8 @@ MeasureCalc::MeasureCalc(MeterWindow* meterWindow, const WCHAR* name) : Measure(
m_LowBound(DEFAULT_LOWER_BOUND), m_LowBound(DEFAULT_LOWER_BOUND),
m_HighBound(DEFAULT_UPPER_BOUND), m_HighBound(DEFAULT_UPPER_BOUND),
m_UpdateRandom(false), m_UpdateRandom(false),
m_UniqueRandom(false), m_UniqueRandom(false)
m_Engine(),
m_Distrubtion()
{ {
std::random_device device;
m_Engine.seed(device());
} }
/* /*
@ -105,11 +104,11 @@ void MeasureCalc::ReadOptions(ConfigParser& parser, const WCHAR* section)
oldUniqueRandom != m_UniqueRandom) oldUniqueRandom != m_UniqueRandom)
{ {
// Reset bounds if |m_LowBound| is greater than |m_HighBound| // Reset bounds if |m_LowBound| is greater than |m_HighBound|
if (m_LowBound > m_HighBound && ( if (m_LowBound > m_HighBound)
oldLowBound != m_LowBound ||
oldHighBound != m_HighBound))
{ {
LogErrorF(this, L"\"LowBound\" (%i) must be less then or equal to \"HighBound\" (%i)", m_LowBound, m_HighBound); LogErrorF(this, L"\"LowBound\" (%i) must be less then or equal to \"HighBound\" (%i)", m_LowBound, m_HighBound);
m_HighBound = m_LowBound;
} }
// Reset the list if the bounds are changed // Reset the list if the bounds are changed
@ -218,10 +217,8 @@ int MeasureCalc::GetRandom()
} }
else else
{ {
std::uniform_int_distribution<int>::param_type params(m_LowBound, m_HighBound); const std::uniform_int_distribution<int> distribution(m_LowBound, m_HighBound);
m_Distrubtion.param(params); return distribution(c_RandomEngine);
m_Distrubtion.reset();
value = m_Distrubtion(m_Engine);
} }
return value; return value;
@ -236,6 +233,6 @@ void MeasureCalc::UpdateUniqueNumberList()
m_UniqueNumbers.push_back(i); m_UniqueNumbers.push_back(i);
} }
std::shuffle(m_UniqueNumbers.begin(), m_UniqueNumbers.end(), m_Engine); std::shuffle(m_UniqueNumbers.begin(), m_UniqueNumbers.end(), c_RandomEngine);
m_UniqueNumbers.shrink_to_fit(); m_UniqueNumbers.shrink_to_fit();
} }

View File

@ -53,8 +53,7 @@ private:
void UpdateUniqueNumberList(); void UpdateUniqueNumberList();
// Uniform Random Number Generator // Uniform Random Number Generator
std::default_random_engine m_Engine; static std::default_random_engine c_RandomEngine;
std::uniform_int_distribution<int> m_Distrubtion;
}; };
#endif #endif