Commit f4669a9d authored by Laurent Montel's avatar Laurent Montel 😁

Port to QRandomGenerator

parent 635ffcab
......@@ -25,6 +25,7 @@
#include <QPainterPath>
#include <QDebug>
#include <QDir>
#include <QRandomGenerator>
#include "utilities.h"
......@@ -74,7 +75,7 @@ void GoldbergEngine::dump_grid_image() {
GBClassicPlugParams GoldbergEngine::initEdge(bool is_straight) {
GBClassicPlugParams r;
r.size_correction = 1.0;
r.flipped = (qrand() % 100 < m_flip_threshold);
r.flipped = (QRandomGenerator::global()->bounded(100) < m_flip_threshold);
r.is_straight = is_straight;
r.is_plugless = false;
r.path_is_rendered = false;
......
......@@ -23,6 +23,7 @@
#include <QDebug>
#include <QPainterPath>
#include <QProcess>
#include <QRandomGenerator>
#include "pointfinder.h"
#include "utilities.h"
......@@ -219,9 +220,10 @@ void IrregularMode::generateGrid(GoldbergEngine *e, int piece_count) const {
if (piece_count < 2) piece_count = 2;
pfinder = new PointFinder(width, height, radius);
auto *generator = QRandomGenerator::global();
for (int i=0; i<piece_count; ++i) {
qreal x = 0.000001 * qreal(qrand() % 1000000) * width;
qreal y = 0.000001 * qreal(qrand() % 1000000) * height;
qreal x = 0.000001 * qreal(generator->bounded(1000000)) * width;
qreal y = 0.000001 * qreal(generator->bounded(1000000)) * height;
pfinder->append(QPointF(x, y));
}
......
......@@ -19,7 +19,7 @@
#include "utilities.h"
#include <QDebug>
#include <QRandomGenerator>
void getBestFit(int &xCount, int &yCount, qreal target_aspect, int approx_count) {
qreal nx_exact = sqrt(approx_count * target_aspect);
......@@ -118,13 +118,14 @@ qreal nonuniform_rand(qreal min, qreal max, qreal sigma, qreal skew) {
qreal randNum;
auto *generator = QRandomGenerator::global();
if (sigma > 0.4247) {
// "wide" distribution, use rejection sampling
qreal x, y;
qreal ssq = 2 * sigma * sigma;
do {
x = 0.000001 * qreal(qrand() % 1000000);
y = 0.000001 * qreal(qrand() % 1000000);
x = 0.000001 * qreal(generator->bounded(1000000));
y = 0.000001 * qreal(generator->bounded(1000000));
} while (y > exp(-(x-0.5)*(x-0.5)/ssq));
randNum = x;
......@@ -136,8 +137,8 @@ qreal nonuniform_rand(qreal min, qreal max, qreal sigma, qreal skew) {
randNum = -1;
do {
do {
u1 = 0.000002 * qreal(qrand() % 1000000) - 1;
u2 = 0.000002 * qreal(qrand() % 1000000) - 1;
u1 = 0.000002 * qreal(generator->bounded(1000000)) - 1;
u2 = 0.000002 * qreal(generator->bounded(1000000)) - 1;
q = u1*u1 + u2*u2;
} while (q>1);
p = sqrt(-2 * log(q) / q) * sigma;
......
......@@ -22,12 +22,13 @@
#include <QPainter>
#include <QPainterPath>
#include <KPluginFactory>
#include <QRandomGenerator>
//BEGIN utility functions
qreal myrand(qreal min, qreal max)
{
const qreal randNum = qreal(qrand() % 10000) / 10000; //a quite random number between 0 and 1
const qreal randNum = qreal(QRandomGenerator::global()->bounded(10000)) / 10000; //a quite random number between 0 and 1
return randNum * (max - min) + min;
}
......@@ -106,6 +107,7 @@ bool JigsawSlicer::run(Pala::SlicerJob* job)
JigsawPlugParams** verticalPlugParams = new JigsawPlugParams*[xCount];
int** horizontalPlugDirections = new int*[xCount]; //+1: male is left, female is right, plug points to the right (-1 is the opposite direction)
int** verticalPlugDirections = new int*[xCount]; //true: male is above female, plug points down
auto *generator = QRandomGenerator::global();
for (int x = 0; x < xCount; ++x)
{
horizontalPlugParams[x] = new JigsawPlugParams[yCount];
......@@ -116,10 +118,10 @@ bool JigsawSlicer::run(Pala::SlicerJob* job)
{
//plugs along X axis
horizontalPlugParams[x][y] = JigsawPlugParams::createRandomParams();
horizontalPlugDirections[x][y] = (qrand() % 2) ? 1 : -1;
horizontalPlugDirections[x][y] = (generator->bounded(2)) ? 1 : -1;
//plugs along Y axis
verticalPlugParams[x][y] = JigsawPlugParams::createRandomParams();
verticalPlugDirections[x][y] = (qrand() % 2) ? 1 : -1;
verticalPlugDirections[x][y] = (generator->bounded(2)) ? 1 : -1;
}
}
//create pieces
......
......@@ -45,6 +45,7 @@
#include <QStandardPaths>
#include <QInputDialog>
#include <QFileDialog>
#include <QRandomGenerator>
#include <KActionCollection>
#include <KLocalizedString>
#include <KMessageBox>
......@@ -1008,6 +1009,7 @@ void Palapeli::GamePlay::loadPiecePositions()
qCDebug(PALAPELI_LOG) << "xMax" << xMax << "x1 y1" << x1 << y1
<< "x2 y2" << x2 << y2;
auto *generator = QRandomGenerator::global();
for (int y = 0; !piecePool.isEmpty(); ++y) {
for (int x = 0; x < xMax && !piecePool.isEmpty(); ++x) {
if ((x >= x1) && (x < x2) &&
......@@ -1016,7 +1018,7 @@ void Palapeli::GamePlay::loadPiecePositions()
}
// Select a random piece.
Palapeli::Piece* piece = piecePool.takeAt(
qrand() % piecePool.count());
generator->bounded(piecePool.count()));
// Place it randomly in grid-cell (x, y).
const QPointF p0(0.0, 0.0);
piece->setPlace(p0, x, y, pieceAreaSize, true);
......
......@@ -27,6 +27,7 @@
#include <QPalette>
#include <QPropertyAnimation>
#include <QPainter> // IDW test.
#include <QRandomGenerator>
void Palapeli::Piece::commonInit(const Palapeli::PieceVisuals& pieceVisuals)
{
......@@ -304,9 +305,10 @@ void Palapeli::Piece::setPlace(const QPointF& topLeft, int x, int y,
if (random) {
int dx = area.width() - pieceSize.width();
int dy = area.height() - pieceSize.height();
auto *generator = QRandomGenerator::global();
areaOffset = QPointF( // Place the piece randomly in the cell.
(dx > 0) ? (qrand() % dx) : 0, // Avoid division by 0.
(dy > 0) ? (qrand() % dy) : 0);
(dx > 0) ? (generator->bounded(dx)) : 0, // Avoid division by 0.
(dy > 0) ? (generator->bounded(dy)) : 0);
}
else {
areaOffset = QPointF( // Center the piece in the cell.
......
......@@ -179,7 +179,7 @@ Palapeli::EventProcessingFlags Palapeli::TriggerMapper::testTrigger(const Palape
}
}
//if execution comes to this point, trigger does not match
return 0;
return {};
}
Palapeli::EventProcessingFlags Palapeli::TriggerMapper::testTrigger(const Palapeli::Trigger& trigger, QMouseEvent* event) const
......@@ -211,7 +211,7 @@ Palapeli::EventProcessingFlags Palapeli::TriggerMapper::testTrigger(const Palape
}
}
//if execution comes to this point, trigger does not match
return 0;
return {};
}
Palapeli::EventProcessingFlags Palapeli::TriggerMapper::testTrigger(const Palapeli::Trigger& trigger, QKeyEvent* event, Qt::MouseButtons buttons) const
......@@ -241,7 +241,7 @@ Palapeli::EventProcessingFlags Palapeli::TriggerMapper::testTrigger(const Palape
}
}
//if execution comes to this point, trigger does not match
return 0;
return {};
}
......@@ -30,7 +30,6 @@
int main(int argc, char** argv)
{
qsrand(time(0));
QApplication app(argc, argv);
KLocalizedString::setApplicationDomain("palapeli");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment