summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarkus Mittendrein <maxmitti@maxmitti.tk>2022-01-15 17:49:24 +0100
committerMarkus Mittendrein <maxmitti@maxmitti.tk>2022-01-15 17:49:24 +0100
commit90ae7d9b7244b3d9a14f03a96e919859580f5a90 (patch)
treebedf4bd96001117b86a783d8514cebc958f7c78f
parentb660519e1915356214c31bd401b01aa750a961a1 (diff)
downloadmanager-master.tar.gz
manager-master.zip
Replace uses of q(s)rand with C++11 <random> stuffHEADmaster
-rw-r--r--src/ProcessManager.cpp4
-rw-r--r--src/crsm.cpp16
-rw-r--r--src/crsm.hpp5
3 files changed, 17 insertions, 8 deletions
diff --git a/src/ProcessManager.cpp b/src/ProcessManager.cpp
index 234d456..e34ebfc 100644
--- a/src/ProcessManager.cpp
+++ b/src/ProcessManager.cpp
@@ -6,6 +6,8 @@
#include <QDebug>
#include <QTextCodec>
+#include <random>
+
ProcessManager::ProcessManager(const QString &newPrefix, const QString &newId, bool useStdErr, QTextCodec* argumentsCodec, QObject *parent) : QObject(parent), prefix(newPrefix), id(newId), useStdErr(useStdErr)
{
if(argumentsCodec == nullptr)
@@ -19,7 +21,7 @@ ProcessManager::ProcessManager(const QString &newPrefix, const QString &newId, b
if(id.isEmpty())
{
- id = QString::number(qrand()) + "-";
+ id = QString::number(std::random_device{}()) + "-";
}
stdIn.setFileName(prefix + id + "stdin");
stdOut.setFileName(prefix + id + "stdout");
diff --git a/src/crsm.cpp b/src/crsm.cpp
index 93dc9d7..b5f0049 100644
--- a/src/crsm.cpp
+++ b/src/crsm.cpp
@@ -19,7 +19,6 @@ CRSM::CRSM(QObject *parent) :
control.setController(this);
parser.addTarget(this);
- qsrand((uint)QDateTime::currentMSecsSinceEpoch());
codec = QTextCodec::codecForName("Windows-1252");
outputBuffer.setFileName(MGMT_BUFFER_FILENAME);
@@ -714,14 +713,15 @@ void CRSM::dccChatRequest(const ClientInfo &client, QString extraArgs)
}
// modified version of http://stackoverflow.com/a/18866593
-QString GetRandomString(int length = 5)
+QString GetRandomString(int length, std::default_random_engine& rng)
{
static const QString possibleCharacters("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
+ std::uniform_int_distribution<int> dist{0, possibleCharacters.length() - 1};
QString randomString;
for(int i=0; i<length; ++i)
{
- int index = qrand() % possibleCharacters.length();
+ const auto index = dist(rng);
QChar nextChar = possibleCharacters.at(index);
randomString.append(nextChar);
}
@@ -748,7 +748,7 @@ void CRSM::newDCCConnection()
QString identifier;
do
{
- identifier = GetRandomString();
+ identifier = GetRandomString(5, rng);
}
while(dccConnectionIdentifiers.contains(identifier));
@@ -775,12 +775,14 @@ void CRSM::disconnectedDCCConnection()
void CRSM::updateNextAutoScens()
{
if(autolist.length() <= 0) return;
+
+ std::uniform_int_distribution<int> autolistDist{0, autolist.length() - 1};
while(nextAutoScens.length() < Config.Hosting.UserListLength || (nextAutoScens.length() <= 0))
{
ScenarioSettings next("");
if(Config.Hosting.RandomizeAuto)
{
- next = autolist.at(qrand() % autolist.length());
+ next = autolist.at(autolistDist(rng));
}
else
{
@@ -789,7 +791,7 @@ void CRSM::updateNextAutoScens()
}
if(next.randomLeague)
{
- next.league = qrand() % 2;
+ next.league = boolRng();
}
nextAutoScens.append(next);
}
@@ -2379,7 +2381,7 @@ void CRSM::watchdog()
if(watchDogString.isEmpty())
{
watchDogTimer.start(Config.Clonk.Server.Watchdog.Timeout * 1000);
- watchDogString = GetRandomString(20);
+ watchDogString = GetRandomString(20, rng);
control.watchdog(watchDogString);
}
else
diff --git a/src/crsm.hpp b/src/crsm.hpp
index e96929f..5a3ba06 100644
--- a/src/crsm.hpp
+++ b/src/crsm.hpp
@@ -1,5 +1,7 @@
#pragma once
+#include <random>
+
#include <QObject>
#include <QProcess>
#include <QTextStream>
@@ -179,6 +181,9 @@ private:
QTimer watchDogTimer;
QString watchDogString;
+ std::default_random_engine rng{std::random_device{}()};
+ std::independent_bits_engine<std::default_random_engine, 1, bool> boolRng{std::default_random_engine{rng()}};
+
bool ok = false;
bool hostingIsErrorDeactivated = false;