2014-04-10 20:11:09 +03:00
|
|
|
/*
|
|
|
|
* CRandomGenerator.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "StdInc.h"
|
|
|
|
#include "CRandomGenerator.h"
|
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2014-04-10 20:11:09 +03:00
|
|
|
CRandomGenerator::CRandomGenerator()
|
|
|
|
{
|
|
|
|
resetSeed();
|
|
|
|
}
|
|
|
|
|
2023-07-11 18:23:14 +02:00
|
|
|
CRandomGenerator::CRandomGenerator(int seed)
|
|
|
|
{
|
|
|
|
setSeed(seed);
|
|
|
|
}
|
|
|
|
|
2014-04-10 20:11:09 +03:00
|
|
|
void CRandomGenerator::setSeed(int seed)
|
|
|
|
{
|
|
|
|
rand.seed(seed);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CRandomGenerator::resetSeed()
|
|
|
|
{
|
|
|
|
boost::hash<std::string> stringHash;
|
|
|
|
auto threadIdHash = stringHash(boost::lexical_cast<std::string>(boost::this_thread::get_id()));
|
2023-03-13 23:26:44 +02:00
|
|
|
setSeed(static_cast<int>(threadIdHash * std::time(nullptr)));
|
2014-04-10 20:11:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
TRandI CRandomGenerator::getIntRange(int lower, int upper)
|
|
|
|
{
|
2024-02-19 00:35:21 +02:00
|
|
|
if (lower <= upper)
|
|
|
|
return std::bind(TIntDist(lower, upper), std::ref(rand));
|
|
|
|
throw std::runtime_error("Invalid range provided: " + std::to_string(lower) + " ... " + std::to_string(upper));
|
2017-07-20 06:08:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
vstd::TRandI64 CRandomGenerator::getInt64Range(int64_t lower, int64_t upper)
|
|
|
|
{
|
2024-02-19 00:35:21 +02:00
|
|
|
if(lower <= upper)
|
|
|
|
return std::bind(TInt64Dist(lower, upper), std::ref(rand));
|
|
|
|
throw std::runtime_error("Invalid range provided: " + std::to_string(lower) + " ... " + std::to_string(upper));
|
2014-04-10 20:11:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int CRandomGenerator::nextInt(int upper)
|
|
|
|
{
|
|
|
|
return getIntRange(0, upper)();
|
|
|
|
}
|
|
|
|
|
|
|
|
int CRandomGenerator::nextInt(int lower, int upper)
|
|
|
|
{
|
|
|
|
return getIntRange(lower, upper)();
|
|
|
|
}
|
|
|
|
|
|
|
|
int CRandomGenerator::nextInt()
|
|
|
|
{
|
|
|
|
return TIntDist()(rand);
|
|
|
|
}
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
vstd::TRand CRandomGenerator::getDoubleRange(double lower, double upper)
|
2014-04-10 20:11:09 +03:00
|
|
|
{
|
2024-02-19 00:35:21 +02:00
|
|
|
if(lower <= upper)
|
|
|
|
return std::bind(TRealDist(lower, upper), std::ref(rand));
|
|
|
|
throw std::runtime_error("Invalid range provided: " + std::to_string(lower) + " ... " + std::to_string(upper));
|
|
|
|
|
2014-04-10 20:11:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
double CRandomGenerator::nextDouble(double upper)
|
|
|
|
{
|
|
|
|
return getDoubleRange(0, upper)();
|
|
|
|
}
|
|
|
|
|
|
|
|
double CRandomGenerator::nextDouble(double lower, double upper)
|
|
|
|
{
|
|
|
|
return getDoubleRange(lower, upper)();
|
|
|
|
}
|
|
|
|
|
|
|
|
double CRandomGenerator::nextDouble()
|
|
|
|
{
|
|
|
|
return TRealDist()(rand);
|
|
|
|
}
|
|
|
|
|
|
|
|
CRandomGenerator & CRandomGenerator::getDefault()
|
|
|
|
{
|
2023-08-20 22:45:41 +02:00
|
|
|
static thread_local CRandomGenerator defaultRand;
|
|
|
|
return defaultRand;
|
2014-04-10 20:11:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
TGenerator & CRandomGenerator::getStdGenerator()
|
|
|
|
{
|
|
|
|
return rand;
|
|
|
|
}
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|