1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-22 22:13:35 +02:00

Integrating changes from PR #4251 - add logging for rng

This commit is contained in:
Ivan Savenko 2024-07-16 15:30:31 +00:00
parent 65eeceb7d7
commit 34b6eaa25e
3 changed files with 15 additions and 0 deletions

View File

@ -193,5 +193,6 @@ extern DLL_LINKAGE vstd::CLoggerBase * logNetwork;
extern DLL_LINKAGE vstd::CLoggerBase * logAi;
extern DLL_LINKAGE vstd::CLoggerBase * logAnim;
extern DLL_LINKAGE vstd::CLoggerBase * logMod;
extern DLL_LINKAGE vstd::CLoggerBase * logRng;
VCMI_LIB_NAMESPACE_END

View File

@ -15,21 +15,25 @@ VCMI_LIB_NAMESPACE_BEGIN
CRandomGenerator::CRandomGenerator()
{
logRng->trace("CRandomGenerator constructed");
resetSeed();
}
CRandomGenerator::CRandomGenerator(int seed)
{
logRng->trace("CRandomGenerator constructed (%d)", seed);
setSeed(seed);
}
void CRandomGenerator::setSeed(int seed)
{
logRng->trace("CRandomGenerator::setSeed (%d)", seed);
rand.seed(seed);
}
void CRandomGenerator::resetSeed()
{
logRng->trace("CRandomGenerator::resetSeed");
boost::hash<std::string> stringHash;
auto threadIdHash = stringHash(boost::lexical_cast<std::string>(boost::this_thread::get_id()));
setSeed(static_cast<int>(threadIdHash * std::time(nullptr)));
@ -37,16 +41,20 @@ void CRandomGenerator::resetSeed()
int CRandomGenerator::nextInt(int upper)
{
logRng->trace("CRandomGenerator::nextInt (%d)", upper);
return nextInt(0, upper);
}
int64_t CRandomGenerator::nextInt64(int64_t upper)
{
logRng->trace("CRandomGenerator::nextInt64 (%d)", upper);
return nextInt64(0, upper);
}
int CRandomGenerator::nextInt(int lower, int upper)
{
logRng->trace("CRandomGenerator::nextInt64 (%d, %d)", lower, upper);
if (lower > upper)
throw std::runtime_error("Invalid range provided: " + std::to_string(lower) + " ... " + std::to_string(upper));
@ -55,17 +63,20 @@ int CRandomGenerator::nextInt(int lower, int upper)
int CRandomGenerator::nextInt()
{
logRng->trace("CRandomGenerator::nextInt64");
return TIntDist()(rand);
}
int CRandomGenerator::nextBinomialInt(int coinsCount, double coinChance)
{
logRng->trace("CRandomGenerator::nextBinomialInt (%d, %f)", coinsCount, coinChance);
std::binomial_distribution<> distribution(coinsCount, coinChance);
return distribution(rand);
}
int64_t CRandomGenerator::nextInt64(int64_t lower, int64_t upper)
{
logRng->trace("CRandomGenerator::nextInt64 (%d, %d)", lower, upper);
if (lower > upper)
throw std::runtime_error("Invalid range provided: " + std::to_string(lower) + " ... " + std::to_string(upper));
@ -74,11 +85,13 @@ int64_t CRandomGenerator::nextInt64(int64_t lower, int64_t upper)
double CRandomGenerator::nextDouble(double upper)
{
logRng->trace("CRandomGenerator::nextDouble (%f)", upper);
return nextDouble(0, upper);
}
double CRandomGenerator::nextDouble(double lower, double upper)
{
logRng->trace("CRandomGenerator::nextDouble (%f, %f)", lower, upper);
if(lower > upper)
throw std::runtime_error("Invalid range provided: " + std::to_string(lower) + " ... " + std::to_string(upper));

View File

@ -96,6 +96,7 @@ DLL_LINKAGE vstd::CLoggerBase * logNetwork = CLogger::getLogger(CLoggerDomain("n
DLL_LINKAGE vstd::CLoggerBase * logAi = CLogger::getLogger(CLoggerDomain("ai"));
DLL_LINKAGE vstd::CLoggerBase * logAnim = CLogger::getLogger(CLoggerDomain("animation"));
DLL_LINKAGE vstd::CLoggerBase * logMod = CLogger::getLogger(CLoggerDomain("mod"));
DLL_LINKAGE vstd::CLoggerBase * logRng = CLogger::getLogger(CLoggerDomain("rng"));
CLogger * CLogger::getLogger(const CLoggerDomain & domain)
{