Merge pull request #1672

pull/411/head
scrawl 7 years ago
commit 1f1f47431f
No known key found for this signature in database
GPG Key ID: 2E6CC3676024C402

@ -1,28 +1,31 @@
#include "rng.hpp" #include "rng.hpp"
#include <cstdlib>
#include <ctime> #include <chrono>
#include <random>
namespace Misc namespace Misc
{ {
std::mt19937 Rng::generator = std::mt19937();
void Rng::init() 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() 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() 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) 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);
} }
} }

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

Loading…
Cancel
Save