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/opengl/qopenglwidget/bubble.cpp | 8 ++++---- examples/opengl/qopenglwidget/glwidget.cpp | 11 ++++++----- examples/opengl/qopenglwidget/mainwindow.cpp | 5 ++++- 3 files changed, 14 insertions(+), 10 deletions(-) (limited to 'examples/opengl/qopenglwidget') diff --git a/examples/opengl/qopenglwidget/bubble.cpp b/examples/opengl/qopenglwidget/bubble.cpp index adf5742f6a6..dbaf460f6f3 100644 --- a/examples/opengl/qopenglwidget/bubble.cpp +++ b/examples/opengl/qopenglwidget/bubble.cpp @@ -109,10 +109,10 @@ void Bubble::drawBubble(QPainter *painter) QColor Bubble::randomColor() { - int red = int(185 + 70.0*qrand()/(RAND_MAX+1.0)); - int green = int(185 + 70.0*qrand()/(RAND_MAX+1.0)); - int blue = int(205 + 50.0*qrand()/(RAND_MAX+1.0)); - int alpha = int(91 + 100.0*qrand()/(RAND_MAX+1.0)); + int red = int(185 + QRandomGenerator::global()->bounded(70)); + int green = int(185 + QRandomGenerator::global()->bounded(70)); + int blue = int(205 + QRandomGenerator::global()->bounded(50)); + int alpha = int(91 + QRandomGenerator::global()->bounded(100)); return QColor(red, green, blue, alpha); } diff --git a/examples/opengl/qopenglwidget/glwidget.cpp b/examples/opengl/qopenglwidget/glwidget.cpp index 40a1258dd53..3fe919f94ba 100644 --- a/examples/opengl/qopenglwidget/glwidget.cpp +++ b/examples/opengl/qopenglwidget/glwidget.cpp @@ -53,6 +53,7 @@ #include #include #include +#include #include #include @@ -420,11 +421,11 @@ void GLWidget::paintGL() void GLWidget::createBubbles(int number) { for (int i = 0; i < number; ++i) { - QPointF position(width()*(0.1 + (0.8*qrand()/(RAND_MAX+1.0))), - height()*(0.1 + (0.8*qrand()/(RAND_MAX+1.0)))); - qreal radius = qMin(width(), height())*(0.0175 + 0.0875*qrand()/(RAND_MAX+1.0)); - QPointF velocity(width()*0.0175*(-0.5 + qrand()/(RAND_MAX+1.0)), - height()*0.0175*(-0.5 + qrand()/(RAND_MAX+1.0))); + QPointF position(width()*(0.1 + QRandomGenerator::global()->bounded(0.8)), + height()*(0.1 + QRandomGenerator::global()->bounded(0.8))); + qreal radius = qMin(width(), height())*(0.0175 + QRandomGenerator::global()->bounded(0.0875)); + QPointF velocity(width()*0.0175*(-0.5 + QRandomGenerator::global()->bounded(1.0)), + height()*0.0175*(-0.5 + QRandomGenerator::global()->bounded(1.0))); m_bubbles.append(new Bubble(position, radius, velocity)); } diff --git a/examples/opengl/qopenglwidget/mainwindow.cpp b/examples/opengl/qopenglwidget/mainwindow.cpp index 1bb2aa2bf0e..4bd123628f0 100644 --- a/examples/opengl/qopenglwidget/mainwindow.cpp +++ b/examples/opengl/qopenglwidget/mainwindow.cpp @@ -56,6 +56,7 @@ #include #include #include +#include #include #include @@ -155,7 +156,9 @@ void MainWindow::addNew() { if (m_nextY == 4) return; - GLWidget *w = new GLWidget(this, false, qRgb(qrand() % 256, qrand() % 256, qrand() % 256)); + GLWidget *w = new GLWidget(this, false, qRgb(QRandomGenerator::global()->bounded(256), + QRandomGenerator::global()->bounded(256), + QRandomGenerator::global()->bounded(256))); m_glWidgets << w; connect(m_timer, &QTimer::timeout, w, QOverload<>::of(&QWidget::update)); m_layout->addWidget(w, m_nextY, m_nextX, 1, 1); -- cgit v1.2.3