2015-03-15 01:07:47 +00:00
|
|
|
#include "rng.hpp"
|
2018-04-08 20:10:14 +00:00
|
|
|
|
|
|
|
#include <chrono>
|
|
|
|
#include <random>
|
2022-03-17 15:35:34 +00:00
|
|
|
#include <sstream>
|
2015-03-15 01:07:47 +00:00
|
|
|
|
2022-03-06 13:58:16 +00:00
|
|
|
namespace Misc::Rng
|
2020-10-13 15:46:32 +00:00
|
|
|
{
|
2022-03-06 13:58:16 +00:00
|
|
|
static Generator sGenerator;
|
2020-10-13 15:46:32 +00:00
|
|
|
|
2022-03-06 13:58:16 +00:00
|
|
|
Generator& getGenerator()
|
2020-10-13 15:46:32 +00:00
|
|
|
{
|
2022-03-06 13:58:16 +00:00
|
|
|
return sGenerator;
|
2020-10-13 15:46:32 +00:00
|
|
|
}
|
2018-04-08 20:10:14 +00:00
|
|
|
|
2022-03-17 15:35:34 +00:00
|
|
|
std::string serialize(const Generator& prng)
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << prng;
|
|
|
|
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
void deserialize(std::string_view data, Generator& prng)
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << data;
|
|
|
|
|
|
|
|
ss.seekg(0);
|
|
|
|
ss >> prng;
|
|
|
|
}
|
|
|
|
|
2022-03-07 10:03:13 +00:00
|
|
|
unsigned int generateDefaultSeed()
|
|
|
|
{
|
|
|
|
auto res = static_cast<unsigned int>(std::chrono::high_resolution_clock::now().time_since_epoch().count());
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2022-03-06 13:58:16 +00:00
|
|
|
void init(unsigned int seed)
|
2015-03-15 01:07:47 +00:00
|
|
|
{
|
2022-03-06 13:58:16 +00:00
|
|
|
sGenerator.seed(seed);
|
2015-03-15 01:07:47 +00:00
|
|
|
}
|
|
|
|
|
2022-03-07 10:03:13 +00:00
|
|
|
float rollProbability()
|
|
|
|
{
|
|
|
|
return std::uniform_real_distribution<float>(0, 1 - std::numeric_limits<float>::epsilon())(getGenerator());
|
|
|
|
}
|
|
|
|
|
2022-03-06 13:58:16 +00:00
|
|
|
float rollProbability(Generator& prng)
|
2015-03-15 01:07:47 +00:00
|
|
|
{
|
2022-03-06 13:58:16 +00:00
|
|
|
return std::uniform_real_distribution<float>(0, 1 - std::numeric_limits<float>::epsilon())(prng);
|
2015-03-15 01:07:47 +00:00
|
|
|
}
|
|
|
|
|
2022-03-07 10:03:13 +00:00
|
|
|
float rollClosedProbability()
|
|
|
|
{
|
|
|
|
return std::uniform_real_distribution<float>(0, 1)(getGenerator());
|
|
|
|
}
|
|
|
|
|
2022-03-06 13:58:16 +00:00
|
|
|
float rollClosedProbability(Generator& prng)
|
2015-03-15 01:07:47 +00:00
|
|
|
{
|
2022-03-06 13:58:16 +00:00
|
|
|
return std::uniform_real_distribution<float>(0, 1)(prng);
|
2015-03-15 01:07:47 +00:00
|
|
|
}
|
|
|
|
|
2022-03-07 10:03:13 +00:00
|
|
|
int rollDice(int max)
|
|
|
|
{
|
|
|
|
return max > 0 ? std::uniform_int_distribution<int>(0, max - 1)(getGenerator()) : 0;
|
|
|
|
}
|
|
|
|
|
2022-03-06 13:58:16 +00:00
|
|
|
int rollDice(int max, Generator& prng)
|
2015-03-15 01:07:47 +00:00
|
|
|
{
|
2022-03-06 13:58:16 +00:00
|
|
|
return max > 0 ? std::uniform_int_distribution<int>(0, max - 1)(prng) : 0;
|
2015-03-15 01:07:47 +00:00
|
|
|
}
|
|
|
|
|
2022-03-07 10:03:13 +00:00
|
|
|
float deviate(float mean, float deviation)
|
2019-02-24 19:41:11 +00:00
|
|
|
{
|
2022-03-07 10:03:13 +00:00
|
|
|
return std::uniform_real_distribution<float>(mean - deviation, mean + deviation)(getGenerator());
|
2019-02-24 19:41:11 +00:00
|
|
|
}
|
2021-03-19 22:23:26 +00:00
|
|
|
|
2022-03-06 13:58:16 +00:00
|
|
|
float deviate(float mean, float deviation, Generator& prng)
|
2021-03-19 22:23:26 +00:00
|
|
|
{
|
2022-03-06 13:58:16 +00:00
|
|
|
return std::uniform_real_distribution<float>(mean - deviation, mean + deviation)(prng);
|
2021-03-19 22:23:26 +00:00
|
|
|
}
|
2022-03-17 15:35:34 +00:00
|
|
|
|
2015-03-15 01:07:47 +00:00
|
|
|
}
|