From 33162c9674e31acbf0bd72056e2a046e4b1441dd Mon Sep 17 00:00:00 2001 From: tWido Date: Fri, 4 Dec 2020 00:38:50 +0100 Subject: [PATCH] refactor(core): use STL random implementation insted of Qt use STL libs to reduce version specific code Closes #6017 --- src/core/core.cpp | 18 +++++------------- src/main.cpp | 8 -------- src/widget/form/settings/privacyform.cpp | 19 +++++-------------- 3 files changed, 10 insertions(+), 35 deletions(-) diff --git a/src/core/core.cpp b/src/core/core.cpp index 3c92a189b..1ebb3ae8c 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -33,18 +33,16 @@ #include "util/strongtype.h" #include -#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)) -#include -#else #include -#endif #include #include #include #include #include +#include #include +#include const QString Core::TOX_EXT = ".tox"; @@ -656,10 +654,6 @@ void Core::onStarted() { ASSERT_CORE_THREAD; -#if (QT_VERSION < QT_VERSION_CHECK(5, 10, 0)) - qsrand(static_cast(QDateTime::currentMSecsSinceEpoch())); -#endif - // One time initialization stuff QString name = getUsername(); if (!name.isEmpty()) { @@ -792,11 +786,9 @@ void Core::bootstrapDht() } int i = 0; -#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)) - static int j = QRandomGenerator::global()->bounded(listSize); -#else - static int j = static_cast((static_cast(qrand()) / static_cast(RAND_MAX+1l)) * listSize); -#endif + std::mt19937 rng(std::chrono::high_resolution_clock::now().time_since_epoch().count()); + std::uniform_int_distribution distribution(0, listSize - 1); + static int j = distribution(rng); // i think the more we bootstrap, the more we jitter because the more we overwrite nodes while (i < 2) { const DhtServer& dhtServer = bootstrapNodesList[j % listSize]; diff --git a/src/main.cpp b/src/main.cpp index 26e3b97d9..3f1df1dfe 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -196,11 +196,6 @@ int main(int argc, char* argv[]) qInstallMessageHandler(logMessageHandler); - // initialize random number generator -#if (QT_VERSION < QT_VERSION_CHECK(5, 15, 0)) - qsrand(time(nullptr)); -#endif - std::unique_ptr a(new QApplication(argc, argv)); #if defined(Q_OS_UNIX) @@ -224,9 +219,6 @@ int main(int argc, char* argv[]) qWarning() << "Couldn't load font"; } -#if (QT_VERSION < QT_VERSION_CHECK(5, 15, 0)) - qsrand(time(nullptr)); -#endif Settings& settings = Settings::getInstance(); QString locale = settings.getTranslation(); // We need to init the resources in the translations_library explicitely. diff --git a/src/widget/form/settings/privacyform.cpp b/src/widget/form/settings/privacyform.cpp index 4cb7aea89..8de2e298e 100644 --- a/src/widget/form/settings/privacyform.cpp +++ b/src/widget/form/settings/privacyform.cpp @@ -23,11 +23,6 @@ #include #include #include -#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)) -#include -#else -#include -#endif #include "src/core/core.h" #include "src/nexus.h" @@ -41,6 +36,9 @@ #include "src/widget/translator.h" #include "src/widget/widget.h" +#include +#include + PrivacyForm::PrivacyForm(Core* _core) : GenericForm(QPixmap(":/img/settings/privacy.png")) , bodyUI(new Ui::PrivacySettings) @@ -106,15 +104,8 @@ void PrivacyForm::on_randomNosapamButton_clicked() { uint32_t newNospam{0}; -#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)) - // guarantees to give a random 32-bit unsigned integer - newNospam = QRandomGenerator::global()->generate(); -#else - qsrand(static_cast(QDateTime::currentMSecsSinceEpoch())); - for (int i = 0; i < 4; ++i) - // Generate byte by byte, as qrand() is guaranteed to have only 15 bits of randomness (RAND_MAX is guaranteed to be 2^15) - newNospam = (newNospam << 8) + (static_cast((static_cast(qrand()) / static_cast(RAND_MAX+1l)) * 256)); -#endif + static std::mt19937 rng(std::chrono::high_resolution_clock::now().time_since_epoch().count()); + newNospam = rng(); core->setNospam(newNospam); bodyUI->nospamLineEdit->setText(core->getSelfId().getNoSpamString());