1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-05 15:55:45 +00:00

Improve random number generation

This commit is contained in:
Capostrophic 2018-04-08 23:10:14 +03:00 committed by Capostrophic
parent 9b8c56761b
commit 48467814d4
2 changed files with 13 additions and 6 deletions

View File

@ -1,28 +1,31 @@
#include "rng.hpp"
#include <cstdlib>
#include <ctime>
#include <chrono>
#include <random>
namespace Misc
{
std::mt19937 Rng::generator = std::mt19937();
void Rng::init()
{
std::srand(static_cast<unsigned int>(std::time(NULL)));
generator.seed(static_cast<unsigned int>(std::chrono::high_resolution_clock::now().time_since_epoch().count()));
}
float Rng::rollProbability()
{
return static_cast<float>(std::rand() / (static_cast<double>(RAND_MAX)+1.0));
return std::uniform_real_distribution<float>(0, 1 - std::numeric_limits<float>::epsilon())(generator);
}
float Rng::rollClosedProbability()
{
return static_cast<float>(std::rand() / static_cast<double>(RAND_MAX));
return std::uniform_real_distribution<float>(0, 1)(generator);
}
int Rng::rollDice(int max)
{
return static_cast<int>((std::rand() / (static_cast<double>(RAND_MAX)+1.0)) * (max));
return std::uniform_int_distribution<int>(0, max - 1)(generator);
}
}

View File

@ -2,6 +2,7 @@
#define OPENMW_COMPONENTS_MISC_RNG_H
#include <cassert>
#include <random>
namespace Misc
{
@ -13,6 +14,9 @@ class Rng
{
public:
/// create a RNG
static std::mt19937 generator;
/// seed the RNG
static void init();