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
|
|
|
}
|
|
|
|
|
|
|
|
int CRandomGenerator::nextInt(int upper)
|
|
|
|
{
|
2024-06-01 15:36:38 +02:00
|
|
|
return nextInt(0, upper);
|
2014-04-10 20:11:09 +03:00
|
|
|
}
|
|
|
|
|
2024-06-01 17:28:17 +02:00
|
|
|
int64_t CRandomGenerator::nextInt64(int64_t upper)
|
|
|
|
{
|
|
|
|
return nextInt64(0, upper);
|
|
|
|
}
|
|
|
|
|
2014-04-10 20:11:09 +03:00
|
|
|
int CRandomGenerator::nextInt(int lower, int upper)
|
|
|
|
{
|
2024-06-01 15:36:38 +02:00
|
|
|
if (lower > upper)
|
|
|
|
throw std::runtime_error("Invalid range provided: " + std::to_string(lower) + " ... " + std::to_string(upper));
|
|
|
|
|
|
|
|
return TIntDist(lower, upper)(rand);
|
2014-04-10 20:11:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int CRandomGenerator::nextInt()
|
|
|
|
{
|
|
|
|
return TIntDist()(rand);
|
|
|
|
}
|
|
|
|
|
2024-06-01 17:28:17 +02:00
|
|
|
int CRandomGenerator::nextBinomialInt(int coinsCount, double coinChance)
|
|
|
|
{
|
|
|
|
std::binomial_distribution<> distribution(coinsCount, coinChance);
|
|
|
|
return distribution(rand);
|
|
|
|
}
|
|
|
|
|
2024-06-01 15:36:38 +02:00
|
|
|
int64_t CRandomGenerator::nextInt64(int64_t lower, int64_t upper)
|
2014-04-10 20:11:09 +03:00
|
|
|
{
|
2024-06-01 15:36:38 +02:00
|
|
|
if (lower > upper)
|
|
|
|
throw std::runtime_error("Invalid range provided: " + std::to_string(lower) + " ... " + std::to_string(upper));
|
2024-02-19 00:35:21 +02:00
|
|
|
|
2024-06-01 15:36:38 +02:00
|
|
|
return TInt64Dist(lower, upper)(rand);
|
2014-04-10 20:11:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
double CRandomGenerator::nextDouble(double upper)
|
|
|
|
{
|
2024-06-01 15:36:38 +02:00
|
|
|
return nextDouble(0, upper);
|
2014-04-10 20:11:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
double CRandomGenerator::nextDouble(double lower, double upper)
|
|
|
|
{
|
2024-06-01 15:36:38 +02:00
|
|
|
if(lower > upper)
|
|
|
|
throw std::runtime_error("Invalid range provided: " + std::to_string(lower) + " ... " + std::to_string(upper));
|
2014-04-10 20:11:09 +03:00
|
|
|
|
2024-06-01 15:36:38 +02:00
|
|
|
return TRealDist(lower, upper)(rand);
|
2014-04-10 20:11:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|