From 48467814d4745cfe27835bd59cd1be6a98f29d58 Mon Sep 17 00:00:00 2001 From: Capostrophic Date: Sun, 8 Apr 2018 23:10:14 +0300 Subject: [PATCH] Improve random number generation --- components/misc/rng.cpp | 15 +++++++++------ components/misc/rng.hpp | 4 ++++ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/components/misc/rng.cpp b/components/misc/rng.cpp index df0fc687e..dfe0eff40 100644 --- a/components/misc/rng.cpp +++ b/components/misc/rng.cpp @@ -1,28 +1,31 @@ #include "rng.hpp" -#include -#include + +#include +#include namespace Misc { + std::mt19937 Rng::generator = std::mt19937(); + void Rng::init() { - std::srand(static_cast(std::time(NULL))); + generator.seed(static_cast(std::chrono::high_resolution_clock::now().time_since_epoch().count())); } float Rng::rollProbability() { - return static_cast(std::rand() / (static_cast(RAND_MAX)+1.0)); + return std::uniform_real_distribution(0, 1 - std::numeric_limits::epsilon())(generator); } float Rng::rollClosedProbability() { - return static_cast(std::rand() / static_cast(RAND_MAX)); + return std::uniform_real_distribution(0, 1)(generator); } int Rng::rollDice(int max) { - return static_cast((std::rand() / (static_cast(RAND_MAX)+1.0)) * (max)); + return std::uniform_int_distribution(0, max - 1)(generator); } } diff --git a/components/misc/rng.hpp b/components/misc/rng.hpp index 01fcdd763..ff56906d9 100644 --- a/components/misc/rng.hpp +++ b/components/misc/rng.hpp @@ -2,6 +2,7 @@ #define OPENMW_COMPONENTS_MISC_RNG_H #include +#include namespace Misc { @@ -13,6 +14,9 @@ class Rng { public: + /// create a RNG + static std::mt19937 generator; + /// seed the RNG static void init();