mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-24 09:54:16 +08:00
Ref T486 Using QRandomGenerator.
This commit is contained in:
@@ -23,6 +23,7 @@
|
|||||||
#include "blackmisc/aviation/callsign.h"
|
#include "blackmisc/aviation/callsign.h"
|
||||||
#include "blackmisc/aviation/liverylist.h"
|
#include "blackmisc/aviation/liverylist.h"
|
||||||
#include "blackmisc/geo/coordinategeodetic.h"
|
#include "blackmisc/geo/coordinategeodetic.h"
|
||||||
|
#include "blackmisc/math/mathutils.h"
|
||||||
#include "blackmisc/pq/units.h"
|
#include "blackmisc/pq/units.h"
|
||||||
#include "blackmisc/test/testing.h"
|
#include "blackmisc/test/testing.h"
|
||||||
#include "blackmisc/directoryutils.h"
|
#include "blackmisc/directoryutils.h"
|
||||||
@@ -47,6 +48,7 @@
|
|||||||
using namespace BlackMisc;
|
using namespace BlackMisc;
|
||||||
using namespace BlackMisc::Aviation;
|
using namespace BlackMisc::Aviation;
|
||||||
using namespace BlackMisc::Geo;
|
using namespace BlackMisc::Geo;
|
||||||
|
using namespace BlackMisc::Math;
|
||||||
using namespace BlackMisc::Network;
|
using namespace BlackMisc::Network;
|
||||||
using namespace BlackMisc::PhysicalQuantities;
|
using namespace BlackMisc::PhysicalQuantities;
|
||||||
using namespace BlackMisc::Simulation;
|
using namespace BlackMisc::Simulation;
|
||||||
@@ -149,7 +151,7 @@ namespace BlackSample
|
|||||||
out << "Copied 10k stations " << number << " times in " << ms << "ms" << endl;
|
out << "Copied 10k stations " << number << " times in " << ms << "ms" << endl;
|
||||||
|
|
||||||
// Regex pattern matching with lists of 10000 strings containing random hex numbers
|
// Regex pattern matching with lists of 10000 strings containing random hex numbers
|
||||||
auto generator = []() { return QString::number(qrand() | (qrand() << 16), 16); };
|
auto generator = []() { return QString::number(CMathUtils::randomGenerator().generate(), 16); };
|
||||||
QStringList strList1, strList2, strList3, strList4;
|
QStringList strList1, strList2, strList3, strList4;
|
||||||
std::generate_n(std::back_inserter(strList1), 100000, generator);
|
std::generate_n(std::back_inserter(strList1), 100000, generator);
|
||||||
std::generate_n(std::back_inserter(strList2), 100000, generator);
|
std::generate_n(std::back_inserter(strList2), 100000, generator);
|
||||||
@@ -350,7 +352,7 @@ namespace BlackSample
|
|||||||
std::generate_n(std::back_inserter(strings), 100000, []
|
std::generate_n(std::back_inserter(strings), 100000, []
|
||||||
{
|
{
|
||||||
QString s;
|
QString s;
|
||||||
std::generate_n(std::back_inserter(s), 10, [] { return chars[qrand() % chars.size()]; });
|
std::generate_n(std::back_inserter(s), 10, [] { return chars[CMathUtils::randomInteger(0, chars.size() - 1)]; });
|
||||||
return s;
|
return s;
|
||||||
});
|
});
|
||||||
QString bigString = strings.join("\n");
|
QString bigString = strings.join("\n");
|
||||||
@@ -698,9 +700,9 @@ namespace BlackSample
|
|||||||
CAircraftModelList models;
|
CAircraftModelList models;
|
||||||
for (int i = 0; i < numberOfModels; ++i)
|
for (int i = 0; i < numberOfModels; ++i)
|
||||||
{
|
{
|
||||||
const auto &aircraftIcao = aircraftIcaos[qrand() % numberOfMemoParts];
|
const auto &aircraftIcao = aircraftIcaos[CMathUtils::randomInteger(0, numberOfMemoParts - 1)];
|
||||||
const auto &livery = liveries[qrand() % numberOfMemoParts];
|
const auto &livery = liveries[CMathUtils::randomInteger(0, numberOfMemoParts - 1)];
|
||||||
const auto &distributor = distributors[qrand() % numberOfMemoParts];
|
const auto &distributor = distributors[CMathUtils::randomInteger(0, numberOfMemoParts - 1)];
|
||||||
models.push_back(CAircraftModel(QString::number(i), CAircraftModel::TypeUnknown, CSimulatorInfo::FSX, QString::number(i), QString::number(i), aircraftIcao, livery));
|
models.push_back(CAircraftModel(QString::number(i), CAircraftModel::TypeUnknown, CSimulatorInfo::FSX, QString::number(i), QString::number(i), aircraftIcao, livery));
|
||||||
models.back().setDistributor(distributor);
|
models.back().setDistributor(distributor);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,7 @@
|
|||||||
#include "blackmisc/directoryutils.h"
|
#include "blackmisc/directoryutils.h"
|
||||||
#include "blackmisc/threadutils.h"
|
#include "blackmisc/threadutils.h"
|
||||||
#include "blackmisc/logmessage.h"
|
#include "blackmisc/logmessage.h"
|
||||||
|
#include "blackmisc/math/mathutils.h"
|
||||||
|
|
||||||
#include <QFlag>
|
#include <QFlag>
|
||||||
#include <Qt>
|
#include <Qt>
|
||||||
@@ -32,6 +33,7 @@ using namespace BlackConfig;
|
|||||||
using namespace BlackMisc;
|
using namespace BlackMisc;
|
||||||
using namespace BlackMisc::Aviation;
|
using namespace BlackMisc::Aviation;
|
||||||
using namespace BlackMisc::Geo;
|
using namespace BlackMisc::Geo;
|
||||||
|
using namespace BlackMisc::Math;
|
||||||
using namespace BlackMisc::Simulation;
|
using namespace BlackMisc::Simulation;
|
||||||
using namespace BlackMisc::Simulation::Settings;
|
using namespace BlackMisc::Simulation::Settings;
|
||||||
using namespace BlackMisc::PhysicalQuantities;
|
using namespace BlackMisc::PhysicalQuantities;
|
||||||
@@ -1104,7 +1106,7 @@ namespace BlackCore
|
|||||||
emit this->requestUiConsoleMessage(dm, true);
|
emit this->requestUiConsoleMessage(dm, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
const int t = 4500 + (qrand() % 1000); // makes sure not always using the same time difference
|
const int t = CMathUtils::randomInteger(4500, 5500); // makes sure not always using the same time difference
|
||||||
const QPointer<ISimulator> myself(this);
|
const QPointer<ISimulator> myself(this);
|
||||||
QTimer::singleShot(t, this, [ = ]
|
QTimer::singleShot(t, this, [ = ]
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -15,6 +15,7 @@
|
|||||||
#include "integersequence.h"
|
#include "integersequence.h"
|
||||||
|
|
||||||
#include <QThreadStorage>
|
#include <QThreadStorage>
|
||||||
|
#include <QRandomGenerator>
|
||||||
#include <QtGlobal>
|
#include <QtGlobal>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
@@ -51,7 +52,7 @@ namespace BlackMisc
|
|||||||
{
|
{
|
||||||
//! \fixme Qt 5.10: Use QRandomGenerator.
|
//! \fixme Qt 5.10: Use QRandomGenerator.
|
||||||
static QThreadStorage<std::mt19937> rng;
|
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();
|
return rng.localData();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -99,32 +99,30 @@ namespace BlackMisc
|
|||||||
return (result >= 0.0) ? result : result + 360.0;
|
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)
|
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(high < INT_MAX, Q_FUNC_INFO, "Cannot add 1");
|
||||||
Q_ASSERT_X(low >= 0 && high >= 0, Q_FUNC_INFO, "Only valid for positive values");
|
return randomGenerator().bounded(low, high + 1);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
double CMathUtils::randomDouble(double max)
|
double CMathUtils::randomDouble(double max)
|
||||||
{
|
{
|
||||||
// on Win system, RAND_MAX is only 16bit, on other systems higher
|
constexpr int MAX(std::min(RAND_MAX - 1, INT_MAX - 1));
|
||||||
static const int MAX(RAND_MAX < INT_MAX ? RAND_MAX - 1 : INT_MAX - 1);
|
|
||||||
const double r = randomInteger(0, MAX);
|
const double r = randomInteger(0, MAX);
|
||||||
return (r / MAX) * max;
|
return (r / MAX) * max;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool CMathUtils::randomBool()
|
||||||
|
{
|
||||||
|
return randomInteger(0, 1);
|
||||||
|
}
|
||||||
|
|
||||||
int CMathUtils::roundToMultipleOf(int value, int divisor)
|
int CMathUtils::roundToMultipleOf(int value, int divisor)
|
||||||
{
|
{
|
||||||
Q_ASSERT(divisor != 0);
|
Q_ASSERT(divisor != 0);
|
||||||
|
|||||||
@@ -14,6 +14,7 @@
|
|||||||
|
|
||||||
#include "blackmisc/blackmiscexport.h"
|
#include "blackmisc/blackmiscexport.h"
|
||||||
|
|
||||||
|
#include <QRandomGenerator>
|
||||||
#include <QtCore/qmath.h>
|
#include <QtCore/qmath.h>
|
||||||
#include <QPair>
|
#include <QPair>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
@@ -115,12 +116,18 @@ namespace BlackMisc
|
|||||||
//! Normalize: 0≤ degrees <360
|
//! Normalize: 0≤ degrees <360
|
||||||
static double normalizeDegrees360(double degrees);
|
static double normalizeDegrees360(double degrees);
|
||||||
|
|
||||||
|
//! Thread-local random generator
|
||||||
|
static QRandomGenerator &randomGenerator();
|
||||||
|
|
||||||
//! Random number between low and high
|
//! Random number between low and high
|
||||||
static int randomInteger(int low, int high);
|
static int randomInteger(int low, int high);
|
||||||
|
|
||||||
//! Random double 0-max
|
//! Random double 0-max
|
||||||
static double randomDouble(double max = 1);
|
static double randomDouble(double max = 1);
|
||||||
|
|
||||||
|
//! Random boolean
|
||||||
|
static bool randomBool();
|
||||||
|
|
||||||
//! Round numToRound to the nearest multiple of divisor
|
//! Round numToRound to the nearest multiple of divisor
|
||||||
static int roundToMultipleOf(int value, int divisor);
|
static int roundToMultipleOf(int value, int divisor);
|
||||||
|
|
||||||
|
|||||||
@@ -16,9 +16,11 @@
|
|||||||
#include "blackmisc/fileutils.h"
|
#include "blackmisc/fileutils.h"
|
||||||
#include "blackmisc/directoryutils.h"
|
#include "blackmisc/directoryutils.h"
|
||||||
#include "blackmisc/variantlist.h"
|
#include "blackmisc/variantlist.h"
|
||||||
|
#include "blackmisc/math/mathutils.h"
|
||||||
|
|
||||||
using namespace BlackMisc::Aviation;
|
using namespace BlackMisc::Aviation;
|
||||||
using namespace BlackMisc::Geo;
|
using namespace BlackMisc::Geo;
|
||||||
|
using namespace BlackMisc::Math;
|
||||||
using namespace BlackMisc::Network;
|
using namespace BlackMisc::Network;
|
||||||
using namespace BlackMisc::Audio;
|
using namespace BlackMisc::Audio;
|
||||||
using namespace BlackMisc::PhysicalQuantities;
|
using namespace BlackMisc::PhysicalQuantities;
|
||||||
@@ -30,7 +32,7 @@ namespace BlackMisc
|
|||||||
{
|
{
|
||||||
int randomIndex(int size)
|
int randomIndex(int size)
|
||||||
{
|
{
|
||||||
return qrand() % size;
|
return CMathUtils::randomInteger(0, size - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
const CServer &CTestData::getTrafficServer()
|
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 <QtGlobal>
|
||||||
#include <QDateTime>
|
#include <QDateTime>
|
||||||
#include <QCryptographicHash>
|
#include <QCryptographicHash>
|
||||||
|
#include <QRandomGenerator>
|
||||||
#include <QDataStream>
|
#include <QDataStream>
|
||||||
|
#include <limits>
|
||||||
|
|
||||||
namespace BlackMisc
|
namespace BlackMisc
|
||||||
{
|
{
|
||||||
@@ -42,9 +44,7 @@ namespace BlackMisc
|
|||||||
m_compressionMode(CompressionAuto),
|
m_compressionMode(CompressionAuto),
|
||||||
m_protectionMode(ProtectionChecksum),
|
m_protectionMode(ProtectionChecksum),
|
||||||
m_lastError(ErrorNoError)
|
m_lastError(ErrorNoError)
|
||||||
{
|
{}
|
||||||
qsrand(uint(QDateTime::currentMSecsSinceEpoch() & 0xFFFF));
|
|
||||||
}
|
|
||||||
|
|
||||||
SimpleCrypt::SimpleCrypt(quint64 key):
|
SimpleCrypt::SimpleCrypt(quint64 key):
|
||||||
m_key(key),
|
m_key(key),
|
||||||
@@ -52,7 +52,6 @@ namespace BlackMisc
|
|||||||
m_protectionMode(ProtectionChecksum),
|
m_protectionMode(ProtectionChecksum),
|
||||||
m_lastError(ErrorNoError)
|
m_lastError(ErrorNoError)
|
||||||
{
|
{
|
||||||
qsrand(uint(QDateTime::currentMSecsSinceEpoch() & 0xFFFF));
|
|
||||||
splitKey();
|
splitKey();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -127,7 +126,8 @@ namespace BlackMisc
|
|||||||
}
|
}
|
||||||
|
|
||||||
//prepend a random char to the string
|
//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;
|
ba = randomChar + integrityProtection + ba;
|
||||||
|
|
||||||
int pos(0);
|
int pos(0);
|
||||||
|
|||||||
@@ -25,6 +25,7 @@
|
|||||||
#include "blackmisc/range.h"
|
#include "blackmisc/range.h"
|
||||||
#include "blackmisc/registermetadata.h"
|
#include "blackmisc/registermetadata.h"
|
||||||
#include "blackmisc/sequence.h"
|
#include "blackmisc/sequence.h"
|
||||||
|
#include "blackmisc/math/mathutils.h"
|
||||||
#include "test.h"
|
#include "test.h"
|
||||||
|
|
||||||
#include <QDateTime>
|
#include <QDateTime>
|
||||||
@@ -44,6 +45,7 @@
|
|||||||
using namespace BlackMisc;
|
using namespace BlackMisc;
|
||||||
using namespace BlackMisc::Aviation;
|
using namespace BlackMisc::Aviation;
|
||||||
using namespace BlackMisc::Geo;
|
using namespace BlackMisc::Geo;
|
||||||
|
using namespace BlackMisc::Math;
|
||||||
using namespace BlackMisc::PhysicalQuantities;
|
using namespace BlackMisc::PhysicalQuantities;
|
||||||
|
|
||||||
namespace BlackMiscTest
|
namespace BlackMiscTest
|
||||||
@@ -369,7 +371,6 @@ namespace BlackMiscTest
|
|||||||
|
|
||||||
void CTestContainers::offsetTimestampList()
|
void CTestContainers::offsetTimestampList()
|
||||||
{
|
{
|
||||||
qsrand(QDateTime::currentDateTime().toTime_t());
|
|
||||||
CAircraftSituationList situations;
|
CAircraftSituationList situations;
|
||||||
static const CCoordinateGeodetic geoPos = CCoordinateGeodetic::fromWgs84("48° 21′ 13″ N", "11° 47′ 09″ E", { 1487, CLengthUnit::ft() });
|
static const CCoordinateGeodetic geoPos = CCoordinateGeodetic::fromWgs84("48° 21′ 13″ N", "11° 47′ 09″ E", { 1487, CLengthUnit::ft() });
|
||||||
qint64 ts = 1000000;
|
qint64 ts = 1000000;
|
||||||
@@ -384,16 +385,14 @@ namespace BlackMiscTest
|
|||||||
s.setMSecsSinceEpoch(ts);
|
s.setMSecsSinceEpoch(ts);
|
||||||
s.setCallsign("CS" + QString::number(i));
|
s.setCallsign("CS" + QString::number(i));
|
||||||
|
|
||||||
if (qrand() % 2 == 0)
|
if (CMathUtils::randomBool())
|
||||||
{
|
{
|
||||||
// 4500-5500
|
dt = CMathUtils::randomInteger(4500, 5500);
|
||||||
dt = 4500 + (qrand() % 1000);
|
|
||||||
s.setTimeOffsetMs(6000);
|
s.setTimeOffsetMs(6000);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// 900-1100
|
dt = CMathUtils::randomInteger(900, 1100);
|
||||||
dt = 900 + (qrand() % 200);
|
|
||||||
s.setTimeOffsetMs(2000);
|
s.setTimeOffsetMs(2000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user