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/corelib/threads/queuedcustomtype/main.cpp | 1 - examples/corelib/threads/queuedcustomtype/renderthread.cpp | 6 ++++-- examples/corelib/threads/semaphores/semaphores.cpp | 3 +-- examples/corelib/threads/waitconditions/waitconditions.cpp | 4 +--- 4 files changed, 6 insertions(+), 8 deletions(-) (limited to 'examples/corelib/threads') diff --git a/examples/corelib/threads/queuedcustomtype/main.cpp b/examples/corelib/threads/queuedcustomtype/main.cpp index 7084b7538a..1f25fafa1b 100644 --- a/examples/corelib/threads/queuedcustomtype/main.cpp +++ b/examples/corelib/threads/queuedcustomtype/main.cpp @@ -126,7 +126,6 @@ int main(int argc, char *argv[]) //! [main start] //! [register meta-type for queued communications] qRegisterMetaType(); //! [register meta-type for queued communications] - qsrand(QTime::currentTime().elapsed()); Window window; window.show(); diff --git a/examples/corelib/threads/queuedcustomtype/renderthread.cpp b/examples/corelib/threads/queuedcustomtype/renderthread.cpp index f894dd587f..67cedf1e74 100644 --- a/examples/corelib/threads/queuedcustomtype/renderthread.cpp +++ b/examples/corelib/threads/queuedcustomtype/renderthread.cpp @@ -50,6 +50,8 @@ #include "renderthread.h" +#include + RenderThread::RenderThread(QObject *parent) : QThread(parent) { @@ -82,9 +84,9 @@ void RenderThread::run() for (int s = size; s > 0; --s) { for (int c = 0; c < 400; ++c) { //![processing the image (start)] - int x1 = qMax(0, (qrand() % m_image.width()) - s/2); + int x1 = qMax(0, QRandomGenerator::global()->bounded(m_image.width()) - s/2); int x2 = qMin(x1 + s/2 + 1, m_image.width()); - int y1 = qMax(0, (qrand() % m_image.height()) - s/2); + int y1 = qMax(0, QRandomGenerator::global()->bounded(m_image.height()) - s/2); int y2 = qMin(y1 + s/2 + 1, m_image.height()); int n = 0; int red = 0; diff --git a/examples/corelib/threads/semaphores/semaphores.cpp b/examples/corelib/threads/semaphores/semaphores.cpp index 37dd4cda20..145624bb0b 100644 --- a/examples/corelib/threads/semaphores/semaphores.cpp +++ b/examples/corelib/threads/semaphores/semaphores.cpp @@ -70,10 +70,9 @@ class Producer : public QThread public: void run() override { - qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); for (int i = 0; i < DataSize; ++i) { freeBytes.acquire(); - buffer[i % BufferSize] = "ACGT"[(int)qrand() % 4]; + buffer[i % BufferSize] = "ACGT"[QRandomGenerator::global()->bounded(4)]; usedBytes.release(); } } diff --git a/examples/corelib/threads/waitconditions/waitconditions.cpp b/examples/corelib/threads/waitconditions/waitconditions.cpp index 9eab28f94c..963ee03a76 100644 --- a/examples/corelib/threads/waitconditions/waitconditions.cpp +++ b/examples/corelib/threads/waitconditions/waitconditions.cpp @@ -76,15 +76,13 @@ public: void run() override { - qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); - for (int i = 0; i < DataSize; ++i) { mutex.lock(); if (numUsedBytes == BufferSize) bufferNotFull.wait(&mutex); mutex.unlock(); - buffer[i % BufferSize] = "ACGT"[(int)qrand() % 4]; + buffer[i % BufferSize] = "ACGT"[QRandomGenerator::global()->bounded(4)]; mutex.lock(); ++numUsedBytes; -- cgit v1.2.3