1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

refactor(core): use STL random implementation insted of Qt

use STL <random> libs to reduce version specific code

Closes #6017
This commit is contained in:
tWido 2020-12-04 00:38:50 +01:00
parent 12fc33ee23
commit 33162c9674
3 changed files with 10 additions and 35 deletions

View File

@ -33,18 +33,16 @@
#include "util/strongtype.h" #include "util/strongtype.h"
#include <QCoreApplication> #include <QCoreApplication>
#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0))
#include <QRandomGenerator>
#else
#include <QDateTime> #include <QDateTime>
#endif
#include <QRegularExpression> #include <QRegularExpression>
#include <QString> #include <QString>
#include <QStringBuilder> #include <QStringBuilder>
#include <QTimer> #include <QTimer>
#include <cassert> #include <cassert>
#include <chrono>
#include <memory> #include <memory>
#include <random>
const QString Core::TOX_EXT = ".tox"; const QString Core::TOX_EXT = ".tox";
@ -656,10 +654,6 @@ void Core::onStarted()
{ {
ASSERT_CORE_THREAD; ASSERT_CORE_THREAD;
#if (QT_VERSION < QT_VERSION_CHECK(5, 10, 0))
qsrand(static_cast<uint>(QDateTime::currentMSecsSinceEpoch()));
#endif
// One time initialization stuff // One time initialization stuff
QString name = getUsername(); QString name = getUsername();
if (!name.isEmpty()) { if (!name.isEmpty()) {
@ -792,11 +786,9 @@ void Core::bootstrapDht()
} }
int i = 0; int i = 0;
#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)) std::mt19937 rng(std::chrono::high_resolution_clock::now().time_since_epoch().count());
static int j = QRandomGenerator::global()->bounded(listSize); std::uniform_int_distribution<int> distribution(0, listSize - 1);
#else static int j = distribution(rng);
static int j = static_cast<int>((static_cast<double>(qrand()) / static_cast<double>(RAND_MAX+1l)) * listSize);
#endif
// i think the more we bootstrap, the more we jitter because the more we overwrite nodes // i think the more we bootstrap, the more we jitter because the more we overwrite nodes
while (i < 2) { while (i < 2) {
const DhtServer& dhtServer = bootstrapNodesList[j % listSize]; const DhtServer& dhtServer = bootstrapNodesList[j % listSize];

View File

@ -196,11 +196,6 @@ int main(int argc, char* argv[])
qInstallMessageHandler(logMessageHandler); qInstallMessageHandler(logMessageHandler);
// initialize random number generator
#if (QT_VERSION < QT_VERSION_CHECK(5, 15, 0))
qsrand(time(nullptr));
#endif
std::unique_ptr<QApplication> a(new QApplication(argc, argv)); std::unique_ptr<QApplication> a(new QApplication(argc, argv));
#if defined(Q_OS_UNIX) #if defined(Q_OS_UNIX)
@ -224,9 +219,6 @@ int main(int argc, char* argv[])
qWarning() << "Couldn't load font"; qWarning() << "Couldn't load font";
} }
#if (QT_VERSION < QT_VERSION_CHECK(5, 15, 0))
qsrand(time(nullptr));
#endif
Settings& settings = Settings::getInstance(); Settings& settings = Settings::getInstance();
QString locale = settings.getTranslation(); QString locale = settings.getTranslation();
// We need to init the resources in the translations_library explicitely. // We need to init the resources in the translations_library explicitely.

View File

@ -23,11 +23,6 @@
#include <QDebug> #include <QDebug>
#include <QFile> #include <QFile>
#include <QMessageBox> #include <QMessageBox>
#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0))
#include <QRandomGenerator>
#else
#include <QDateTime>
#endif
#include "src/core/core.h" #include "src/core/core.h"
#include "src/nexus.h" #include "src/nexus.h"
@ -41,6 +36,9 @@
#include "src/widget/translator.h" #include "src/widget/translator.h"
#include "src/widget/widget.h" #include "src/widget/widget.h"
#include <chrono>
#include <random>
PrivacyForm::PrivacyForm(Core* _core) PrivacyForm::PrivacyForm(Core* _core)
: GenericForm(QPixmap(":/img/settings/privacy.png")) : GenericForm(QPixmap(":/img/settings/privacy.png"))
, bodyUI(new Ui::PrivacySettings) , bodyUI(new Ui::PrivacySettings)
@ -106,15 +104,8 @@ void PrivacyForm::on_randomNosapamButton_clicked()
{ {
uint32_t newNospam{0}; uint32_t newNospam{0};
#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)) static std::mt19937 rng(std::chrono::high_resolution_clock::now().time_since_epoch().count());
// guarantees to give a random 32-bit unsigned integer newNospam = rng();
newNospam = QRandomGenerator::global()->generate();
#else
qsrand(static_cast<uint>(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<int>((static_cast<double>(qrand()) / static_cast<double>(RAND_MAX+1l)) * 256));
#endif
core->setNospam(newNospam); core->setNospam(newNospam);
bodyUI->nospamLineEdit->setText(core->getSelfId().getNoSpamString()); bodyUI->nospamLineEdit->setText(core->getSelfId().getNoSpamString());