mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-03-31 21:15:33 +08:00
Ref T486 Using QRandomGenerator.
This commit is contained in:
@@ -15,6 +15,7 @@
|
||||
#include "integersequence.h"
|
||||
|
||||
#include <QThreadStorage>
|
||||
#include <QRandomGenerator>
|
||||
#include <QtGlobal>
|
||||
#include <algorithm>
|
||||
#include <iterator>
|
||||
@@ -51,7 +52,7 @@ namespace BlackMisc
|
||||
{
|
||||
//! \fixme Qt 5.10: Use QRandomGenerator.
|
||||
static QThreadStorage<std::mt19937> rng;
|
||||
if (rng.hasLocalData()) { rng.setLocalData(std::mt19937(static_cast<std::mt19937::result_type>(qrand()))); }
|
||||
if (rng.hasLocalData()) { rng.setLocalData(std::mt19937(static_cast<std::mt19937::result_type>(QRandomGenerator::global()->generate()))); }
|
||||
return rng.localData();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,32 +99,30 @@ namespace BlackMisc
|
||||
return (result >= 0.0) ? result : result + 360.0;
|
||||
}
|
||||
|
||||
QRandomGenerator &CMathUtils::randomGenerator()
|
||||
{
|
||||
thread_local QRandomGenerator rng(QRandomGenerator::global()->generate());
|
||||
return rng;
|
||||
}
|
||||
|
||||
int CMathUtils::randomInteger(int low, int high)
|
||||
{
|
||||
static QThreadStorage<uint> seeds;
|
||||
Q_ASSERT_X(high < INT_MAX, Q_FUNC_INFO, "Cannot add 1");
|
||||
Q_ASSERT_X(low >= 0 && high >= 0, Q_FUNC_INFO, "Only valid for positive values");
|
||||
if (!seeds.hasLocalData())
|
||||
{
|
||||
// seed is per thread!
|
||||
const uint seed = static_cast<uint>(QTime::currentTime().msec());
|
||||
qsrand(seed);
|
||||
seeds.setLocalData(seed);
|
||||
}
|
||||
const int r(qrand());
|
||||
const int mod = (high + 1) - low;
|
||||
Q_ASSERT_X(mod <= RAND_MAX, Q_FUNC_INFO, "RAND_MAX exceeded");
|
||||
return (r % mod) + low;
|
||||
return randomGenerator().bounded(low, high + 1);
|
||||
}
|
||||
|
||||
double CMathUtils::randomDouble(double max)
|
||||
{
|
||||
// on Win system, RAND_MAX is only 16bit, on other systems higher
|
||||
static const int MAX(RAND_MAX < INT_MAX ? RAND_MAX - 1 : INT_MAX - 1);
|
||||
constexpr int MAX(std::min(RAND_MAX - 1, INT_MAX - 1));
|
||||
const double r = randomInteger(0, MAX);
|
||||
return (r / MAX) * max;
|
||||
}
|
||||
|
||||
bool CMathUtils::randomBool()
|
||||
{
|
||||
return randomInteger(0, 1);
|
||||
}
|
||||
|
||||
int CMathUtils::roundToMultipleOf(int value, int divisor)
|
||||
{
|
||||
Q_ASSERT(divisor != 0);
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
|
||||
#include "blackmisc/blackmiscexport.h"
|
||||
|
||||
#include <QRandomGenerator>
|
||||
#include <QtCore/qmath.h>
|
||||
#include <QPair>
|
||||
#include <cmath>
|
||||
@@ -115,12 +116,18 @@ namespace BlackMisc
|
||||
//! Normalize: 0≤ degrees <360
|
||||
static double normalizeDegrees360(double degrees);
|
||||
|
||||
//! Thread-local random generator
|
||||
static QRandomGenerator &randomGenerator();
|
||||
|
||||
//! Random number between low and high
|
||||
static int randomInteger(int low, int high);
|
||||
|
||||
//! Random double 0-max
|
||||
static double randomDouble(double max = 1);
|
||||
|
||||
//! Random boolean
|
||||
static bool randomBool();
|
||||
|
||||
//! Round numToRound to the nearest multiple of divisor
|
||||
static int roundToMultipleOf(int value, int divisor);
|
||||
|
||||
|
||||
@@ -16,9 +16,11 @@
|
||||
#include "blackmisc/fileutils.h"
|
||||
#include "blackmisc/directoryutils.h"
|
||||
#include "blackmisc/variantlist.h"
|
||||
#include "blackmisc/math/mathutils.h"
|
||||
|
||||
using namespace BlackMisc::Aviation;
|
||||
using namespace BlackMisc::Geo;
|
||||
using namespace BlackMisc::Math;
|
||||
using namespace BlackMisc::Network;
|
||||
using namespace BlackMisc::Audio;
|
||||
using namespace BlackMisc::PhysicalQuantities;
|
||||
@@ -30,7 +32,7 @@ namespace BlackMisc
|
||||
{
|
||||
int randomIndex(int size)
|
||||
{
|
||||
return qrand() % size;
|
||||
return CMathUtils::randomInteger(0, size - 1);
|
||||
}
|
||||
|
||||
const CServer &CTestData::getTrafficServer()
|
||||
|
||||
10
src/blackmisc/thirdparty/simplecrypt.cpp
vendored
10
src/blackmisc/thirdparty/simplecrypt.cpp
vendored
@@ -31,7 +31,9 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
#include <QtGlobal>
|
||||
#include <QDateTime>
|
||||
#include <QCryptographicHash>
|
||||
#include <QRandomGenerator>
|
||||
#include <QDataStream>
|
||||
#include <limits>
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
@@ -42,9 +44,7 @@ namespace BlackMisc
|
||||
m_compressionMode(CompressionAuto),
|
||||
m_protectionMode(ProtectionChecksum),
|
||||
m_lastError(ErrorNoError)
|
||||
{
|
||||
qsrand(uint(QDateTime::currentMSecsSinceEpoch() & 0xFFFF));
|
||||
}
|
||||
{}
|
||||
|
||||
SimpleCrypt::SimpleCrypt(quint64 key):
|
||||
m_key(key),
|
||||
@@ -52,7 +52,6 @@ namespace BlackMisc
|
||||
m_protectionMode(ProtectionChecksum),
|
||||
m_lastError(ErrorNoError)
|
||||
{
|
||||
qsrand(uint(QDateTime::currentMSecsSinceEpoch() & 0xFFFF));
|
||||
splitKey();
|
||||
}
|
||||
|
||||
@@ -127,7 +126,8 @@ namespace BlackMisc
|
||||
}
|
||||
|
||||
//prepend a random char to the string
|
||||
char randomChar = char(qrand() & 0xFF);
|
||||
thread_local QRandomGenerator rng(QRandomGenerator::global()->generate());
|
||||
char randomChar = static_cast<char>(rng.bounded(static_cast<int>(std::numeric_limits<char>::min()), static_cast<int>(std::numeric_limits<char>::max()) + 1));
|
||||
ba = randomChar + integrityProtection + ba;
|
||||
|
||||
int pos(0);
|
||||
|
||||
Reference in New Issue
Block a user