From 19b0ce5daa31e2ffebfcf2701143742302f1deb4 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Thu, 13 Apr 2017 21:13:52 -0700 Subject: Change almost all other uses of qrand() to QRandomGenerator The vast majority is actually switched to QRandomGenerator::bounded(), which gives a mostly uniform distribution over the [0, bound) range. There are very few floating point cases left, as many of those that did use floating point did not need to, after all. (I did leave some that were too ugly for me to understand) This commit also found a couple of calls to rand() instead of qrand(). This commit does not include changes to SSL code that continues to use qrand() (job for someone else): src/network/ssl/qsslkey_qt.cpp src/network/ssl/qsslsocket_mac.cpp tests/auto/network/ssl/qsslsocket/tst_qsslsocket.cpp Change-Id: Icd0e0d4b27cb4e5eb892fffd14b5285d43f4afbf Reviewed-by: Lars Knoll --- examples/widgets/animation/animatedtiles/main.cpp | 5 +++-- examples/widgets/animation/moveblocks/main.cpp | 4 +--- examples/widgets/animation/stickman/lifecycle.cpp | 3 +-- examples/widgets/animation/sub-attaq/main.cpp | 2 -- examples/widgets/animation/sub-attaq/states.cpp | 7 ++++--- examples/widgets/animation/sub-attaq/submarine_p.h | 5 +++-- 6 files changed, 12 insertions(+), 14 deletions(-) (limited to 'examples/widgets/animation') diff --git a/examples/widgets/animation/animatedtiles/main.cpp b/examples/widgets/animation/animatedtiles/main.cpp index 0511fe8162..89b5b67f8a 100644 --- a/examples/widgets/animation/animatedtiles/main.cpp +++ b/examples/widgets/animation/animatedtiles/main.cpp @@ -50,6 +50,7 @@ #include #include +#include #include class Pixmap : public QObject, public QGraphicsPixmapItem @@ -202,8 +203,8 @@ int main(int argc, char **argv) // Random randomState->assignProperty(item, "pos", - QPointF(-250 + qrand() % 500, - -250 + qrand() % 500)); + QPointF(-250 + QRandomGenerator::global()->bounded(500), + -250 + QRandomGenerator::global()->bounded(500))); // Tiled tiledState->assignProperty(item, "pos", diff --git a/examples/widgets/animation/moveblocks/main.cpp b/examples/widgets/animation/moveblocks/main.cpp index a9b95808a5..6d17696108 100644 --- a/examples/widgets/animation/moveblocks/main.cpp +++ b/examples/widgets/animation/moveblocks/main.cpp @@ -125,7 +125,7 @@ public: void onEntry(QEvent *) override { int n; - while ((n = (qrand() % m_stateCount + 1)) == m_lastIndex) + while ((n = QRandomGenerator::global()->bounded(m_stateCount) + 1) == m_lastIndex) { } m_lastIndex = n; machine()->postEvent(new StateSwitchEvent(n)); @@ -323,8 +323,6 @@ int main(int argc, char **argv) window.resize(300, 300); window.show(); - qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); - return app.exec(); } diff --git a/examples/widgets/animation/stickman/lifecycle.cpp b/examples/widgets/animation/stickman/lifecycle.cpp index 253af22b2d..dbe9a299b4 100644 --- a/examples/widgets/animation/stickman/lifecycle.cpp +++ b/examples/widgets/animation/stickman/lifecycle.cpp @@ -91,13 +91,12 @@ public: : QEventTransition(this, QEvent::Timer) { setTargetState(target); - qsrand((uint)QDateTime::currentSecsSinceEpoch()); startTimer(1000); } bool eventTest(QEvent *e) override { - return QEventTransition::eventTest(e) && ((qrand() % 50) == 0); + return QEventTransition::eventTest(e) && QRandomGenerator::global()->bounded(50) == 0; } }; //! [4] diff --git a/examples/widgets/animation/sub-attaq/main.cpp b/examples/widgets/animation/sub-attaq/main.cpp index f65ca7be18..9b28d8c40f 100644 --- a/examples/widgets/animation/sub-attaq/main.cpp +++ b/examples/widgets/animation/sub-attaq/main.cpp @@ -57,8 +57,6 @@ int main(int argc, char *argv[]) QApplication app(argc, argv); Q_INIT_RESOURCE(subattaq); - qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); - MainWindow w; w.show(); diff --git a/examples/widgets/animation/sub-attaq/states.cpp b/examples/widgets/animation/sub-attaq/states.cpp index 21cff048e7..e19704db7b 100644 --- a/examples/widgets/animation/sub-attaq/states.cpp +++ b/examples/widgets/animation/sub-attaq/states.cpp @@ -64,6 +64,7 @@ #include #include #include +#include PlayState::PlayState(GraphicsScene *scene, QState *parent) : QState(parent), @@ -193,12 +194,12 @@ void LevelState::initializeLevel() for (int j = 0; j < subContent.second; ++j ) { SubMarine *sub = new SubMarine(submarineDesc.type, submarineDesc.name, submarineDesc.points); scene->addItem(sub); - int random = (qrand() % 15 + 1); + int random = QRandomGenerator::global()->bounded(15) + 1; qreal x = random == 13 || random == 5 ? 0 : scene->width() - sub->size().width(); - qreal y = scene->height() -(qrand() % 150 + 1) - sub->size().height(); + qreal y = scene->height() -(QRandomGenerator::global()->bounded(150) + 1) - sub->size().height(); sub->setPos(x,y); sub->setCurrentDirection(x == 0 ? SubMarine::Right : SubMarine::Left); - sub->setCurrentSpeed(qrand() % 3 + 1); + sub->setCurrentSpeed(QRandomGenerator::global()->bounded(3) + 1); } } } diff --git a/examples/widgets/animation/sub-attaq/submarine_p.h b/examples/widgets/animation/sub-attaq/submarine_p.h index b8d5532962..698b4b494f 100644 --- a/examples/widgets/animation/sub-attaq/submarine_p.h +++ b/examples/widgets/animation/sub-attaq/submarine_p.h @@ -69,6 +69,7 @@ //Qt #include +#include #include //This state is describing when the boat is moving right @@ -88,8 +89,8 @@ public: protected slots: void onAnimationMovementValueChanged(const QVariant &) { - if (qrand() % 200 + 1 == 3) - submarine->launchTorpedo(qrand() % 3 + 1); + if (QRandomGenerator::global()->bounded(200) + 1 == 3) + submarine->launchTorpedo(QRandomGenerator::global()->bounded(3) + 1); } protected: -- cgit v1.2.3