summaryrefslogtreecommitdiffstats
path: root/examples/widgets/animation
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2017-04-13 21:13:52 -0700
committerLars Knoll <lars.knoll@qt.io>2017-11-08 09:14:03 +0000
commit19b0ce5daa31e2ffebfcf2701143742302f1deb4 (patch)
tree730cccd80947c60ee4872e16f71d4d48d906c354 /examples/widgets/animation
parent59c5f7bd9da63621887260fc45e6669282d71ecd (diff)
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 <lars.knoll@qt.io>
Diffstat (limited to 'examples/widgets/animation')
-rw-r--r--examples/widgets/animation/animatedtiles/main.cpp5
-rw-r--r--examples/widgets/animation/moveblocks/main.cpp4
-rw-r--r--examples/widgets/animation/stickman/lifecycle.cpp3
-rw-r--r--examples/widgets/animation/sub-attaq/main.cpp2
-rw-r--r--examples/widgets/animation/sub-attaq/states.cpp7
-rw-r--r--examples/widgets/animation/sub-attaq/submarine_p.h5
6 files changed, 12 insertions, 14 deletions
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 <QtWidgets>
#include <QtCore/qmath.h>
+#include <QtCore/qrandom.h>
#include <QtCore/qstate.h>
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 <QtCore/QStateMachine>
#include <QtWidgets/QKeyEventTransition>
#include <QtCore/QFinalState>
+#include <QtCore/QRandomGenerator>
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 <QtCore/QPropertyAnimation>
+#include <QtCore/QRandomGenerator>
#include <QtWidgets/QGraphicsScene>
//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: