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 --- .../plugandpaint/plugins/basictools/basictoolsplugin.cpp | 2 +- examples/widgets/tools/undo/mainwindow.cpp | 11 ++++++----- examples/widgets/tools/undoframework/diagramitem.cpp | 3 +-- 3 files changed, 8 insertions(+), 8 deletions(-) (limited to 'examples/widgets/tools') diff --git a/examples/widgets/tools/plugandpaint/plugins/basictools/basictoolsplugin.cpp b/examples/widgets/tools/plugandpaint/plugins/basictools/basictoolsplugin.cpp index 740d007d44..92620ddd8c 100644 --- a/examples/widgets/tools/plugandpaint/plugins/basictools/basictoolsplugin.cpp +++ b/examples/widgets/tools/plugandpaint/plugins/basictools/basictoolsplugin.cpp @@ -101,7 +101,7 @@ QRect BasicToolsPlugin::mouseMove(const QString &brush, QPainter &painter, thickness, thickness); } } else if (brush == tr("Random Letters")) { - QChar ch('A' + (qrand() % 26)); + QChar ch(QRandomGenerator::global()->bounded('A', 'Z' + 1)); QFont biggerFont = painter.font(); biggerFont.setBold(true); diff --git a/examples/widgets/tools/undo/mainwindow.cpp b/examples/widgets/tools/undo/mainwindow.cpp index aa570caa80..5976163f3f 100644 --- a/examples/widgets/tools/undo/mainwindow.cpp +++ b/examples/widgets/tools/undo/mainwindow.cpp @@ -52,6 +52,7 @@ #include #include #include +#include #include #include #include "document.h" @@ -321,7 +322,7 @@ void MainWindow::newDocument() static QColor randomColor() { - int r = (int) (3.0*(rand()/(RAND_MAX + 1.0))); + int r = QRandomGenerator::global()->bounded(3); switch (r) { case 0: return Qt::red; @@ -337,10 +338,10 @@ static QRect randomRect(const QSize &s) { QSize min = Shape::minSize; - int left = (int) ((0.0 + s.width() - min.width())*(rand()/(RAND_MAX + 1.0))); - int top = (int) ((0.0 + s.height() - min.height())*(rand()/(RAND_MAX + 1.0))); - int width = (int) ((0.0 + s.width() - left - min.width())*(rand()/(RAND_MAX + 1.0))) + min.width(); - int height = (int) ((0.0 + s.height() - top - min.height())*(rand()/(RAND_MAX + 1.0))) + min.height(); + int left = (int) ((0.0 + s.width() - min.width())*(QRandomGenerator::global()->bounded(1.0))); + int top = (int) ((0.0 + s.height() - min.height())*(QRandomGenerator::global()->bounded(1.0))); + int width = (int) ((0.0 + s.width() - left - min.width())*(QRandomGenerator::global()->bounded(1.0))) + min.width(); + int height = (int) ((0.0 + s.height() - top - min.height())*(QRandomGenerator::global()->bounded(1.0))) + min.height(); return QRect(left, top, width, height); } diff --git a/examples/widgets/tools/undoframework/diagramitem.cpp b/examples/widgets/tools/undoframework/diagramitem.cpp index 754baa2377..723645c9b2 100644 --- a/examples/widgets/tools/undoframework/diagramitem.cpp +++ b/examples/widgets/tools/undoframework/diagramitem.cpp @@ -65,8 +65,7 @@ DiagramItem::DiagramItem(DiagramType diagramType, QGraphicsItem *item) setPolygon(trianglePolygon); } - QColor color(static_cast(qrand()) % 256, - static_cast(qrand()) % 256, static_cast(qrand()) % 256); + QColor color(QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256)); QBrush brush(color); setBrush(brush); setFlag(QGraphicsItem::ItemIsSelectable); -- cgit v1.2.3