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 <QCoreApplication>
#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0))
#include <QRandomGenerator>
#else
#include <QDateTime>
#endif
#include <QRegularExpression>
#include <QString>
#include <QStringBuilder>
#include <QTimer>
#include <cassert>
#include <chrono>
#include <memory>
#include <random>
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<uint>(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<int>((static_cast<double>(qrand()) / static_cast<double>(RAND_MAX+1l)) * listSize);
#endif
std::mt19937 rng(std::chrono::high_resolution_clock::now().time_since_epoch().count());
std::uniform_int_distribution<int> 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];

View File

@ -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<QApplication> 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.

View File

@ -23,11 +23,6 @@
#include <QDebug>
#include <QFile>
#include <QMessageBox>
#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0))
#include <QRandomGenerator>
#else
#include <QDateTime>
#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 <chrono>
#include <random>
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<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
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());