From 852259b33141281693efa8ccfec4d62affdaab82 Mon Sep 17 00:00:00 2001 From: Renaud G Date: Sun, 23 Oct 2016 23:30:54 +0200 Subject: -optimizing the random generation. --- die.cpp | 12 ++++++------ die.h | 2 ++ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/die.cpp b/die.cpp index 4dd61de..e423268 100644 --- a/die.cpp +++ b/die.cpp @@ -29,11 +29,12 @@ Die::Die() : m_hasValue(false),m_displayStatus(false),m_highlighted(true),m_base(1),m_color("")//,m_mt(m_randomDevice) { - uint seed = quintptr(this) + QDateTime::currentDateTime().toMSecsSinceEpoch(); - - qsrand(seed); +// uint seed = quintptr(this) + QDateTime::currentDateTime().toMSecsSinceEpoch(); + // qsrand(seed); + auto seed = std::chrono::high_resolution_clock::now().time_since_epoch().count(); + m_rng = std::mt19937(quintptr(this)+seed); } Die::Die(const Die& die) @@ -103,10 +104,9 @@ void Die::roll(bool adding) if(m_faces!=0) { //quint64 value=(qrand()%m_faces)+m_base; - auto seed = std::chrono::high_resolution_clock::now().time_since_epoch().count(); - std::mt19937 rng(quintptr(this)+seed); + std::uniform_int_distribution dist(m_base,m_faces); - qint64 value = dist(rng); + qint64 value = dist(m_rng); if((adding)||(m_rollResult.isEmpty())) { insertRollValue(value); diff --git a/die.h b/die.h index e29f10d..6f3af8c 100644 --- a/die.h +++ b/die.h @@ -138,6 +138,8 @@ private: quint64 m_faces; qint64 m_base; QString m_color; + + std::mt19937 m_rng; }; -- cgit v1.2.3-70-g09d2