2015-03-15 01:07:47 +00:00
|
|
|
#include "rng.hpp"
|
2018-04-08 20:10:14 +00:00
|
|
|
|
|
|
|
#include <chrono>
|
|
|
|
#include <random>
|
2015-03-15 01:07:47 +00:00
|
|
|
|
2020-10-13 15:46:32 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
Misc::Rng::Seed sSeed;
|
|
|
|
}
|
|
|
|
|
2015-04-22 15:58:55 +00:00
|
|
|
namespace Misc
|
|
|
|
{
|
2015-03-15 01:07:47 +00:00
|
|
|
|
2020-10-13 15:46:32 +00:00
|
|
|
Rng::Seed::Seed() {}
|
|
|
|
|
|
|
|
Rng::Seed::Seed(unsigned int seed)
|
|
|
|
{
|
|
|
|
mGenerator.seed(seed);
|
|
|
|
}
|
|
|
|
|
|
|
|
Rng::Seed& Rng::getSeed()
|
|
|
|
{
|
|
|
|
return sSeed;
|
|
|
|
}
|
2018-04-08 20:10:14 +00:00
|
|
|
|
2019-02-24 19:41:11 +00:00
|
|
|
void Rng::init(unsigned int seed)
|
2015-03-15 01:07:47 +00:00
|
|
|
{
|
2020-10-13 15:46:32 +00:00
|
|
|
sSeed.mGenerator.seed(seed);
|
2015-03-15 01:07:47 +00:00
|
|
|
}
|
|
|
|
|
2020-10-13 15:46:32 +00:00
|
|
|
float Rng::rollProbability(Seed& seed)
|
2015-03-15 01:07:47 +00:00
|
|
|
{
|
2020-10-16 19:54:26 +00:00
|
|
|
return std::uniform_real_distribution<float>(0, 1 - std::numeric_limits<float>::epsilon())(seed.mGenerator);
|
2015-03-15 01:07:47 +00:00
|
|
|
}
|
|
|
|
|
2020-10-13 15:46:32 +00:00
|
|
|
float Rng::rollClosedProbability(Seed& seed)
|
2015-03-15 01:07:47 +00:00
|
|
|
{
|
2020-10-16 19:54:26 +00:00
|
|
|
return std::uniform_real_distribution<float>(0, 1)(seed.mGenerator);
|
2015-03-15 01:07:47 +00:00
|
|
|
}
|
|
|
|
|
2020-10-13 15:46:32 +00:00
|
|
|
int Rng::rollDice(int max, Seed& seed)
|
2015-03-15 01:07:47 +00:00
|
|
|
{
|
2020-10-16 19:54:26 +00:00
|
|
|
return max > 0 ? std::uniform_int_distribution<int>(0, max - 1)(seed.mGenerator) : 0;
|
2015-03-15 01:07:47 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 19:41:11 +00:00
|
|
|
unsigned int Rng::generateDefaultSeed()
|
|
|
|
{
|
|
|
|
return static_cast<unsigned int>(std::chrono::high_resolution_clock::now().time_since_epoch().count());
|
|
|
|
}
|
2021-03-19 22:23:26 +00:00
|
|
|
|
|
|
|
float Rng::deviate(float mean, float deviation, Seed& seed)
|
|
|
|
{
|
|
|
|
return std::uniform_real_distribution<float>(mean - deviation, mean + deviation)(seed.mGenerator);
|
|
|
|
}
|
2015-03-15 01:07:47 +00:00
|
|
|
}
|