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 --- .../corelib/tools/qalgorithms/tst_qalgorithms.cpp | 2 +- .../benchmarks/corelib/tools/qcryptographichash/main.cpp | 6 +++--- .../GraphicsViewBenchmark/widgets/dummydatagen.cpp | 16 ++++++++-------- .../qgraphicsview/benchapps/moveItems/main.cpp | 2 +- .../graphicsview/qgraphicswidget/tst_qgraphicswidget.cpp | 5 ++++- tests/benchmarks/gui/painting/qtbench/benchmarktests.h | 9 +++++---- .../network/access/qnetworkreply/tst_qnetworkreply.cpp | 3 ++- 7 files changed, 24 insertions(+), 19 deletions(-) (limited to 'tests/benchmarks') diff --git a/tests/benchmarks/corelib/tools/qalgorithms/tst_qalgorithms.cpp b/tests/benchmarks/corelib/tools/qalgorithms/tst_qalgorithms.cpp index 11e559a298..a4ad3a08a8 100644 --- a/tests/benchmarks/corelib/tools/qalgorithms/tst_qalgorithms.cpp +++ b/tests/benchmarks/corelib/tools/qalgorithms/tst_qalgorithms.cpp @@ -57,7 +57,7 @@ QVector generateData(QString dataSetType, const int length) QVector container; if (dataSetType == "Random") { for (int i = 0; i < length; ++i) - container.append(rand()); + container.append(QRandomGenerator::global()->generate()); } else if (dataSetType == "Ascending") { for (int i = 0; i < length; ++i) container.append(i); diff --git a/tests/benchmarks/corelib/tools/qcryptographichash/main.cpp b/tests/benchmarks/corelib/tools/qcryptographichash/main.cpp index 507e2af708..1d414161d1 100644 --- a/tests/benchmarks/corelib/tools/qcryptographichash/main.cpp +++ b/tests/benchmarks/corelib/tools/qcryptographichash/main.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 Intel Corporation. +** Copyright (C) 2017 Intel Corporation. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtCore module of the Qt Toolkit. @@ -29,6 +29,7 @@ #include #include #include +#include #include #include @@ -102,9 +103,8 @@ tst_bench_QCryptographicHash::tst_bench_QCryptographicHash() } else #endif { - qsrand(time(NULL)); for (int i = 0; i < MaxBlockSize; ++i) - blockOfData[i] = qrand(); + blockOfData[i] = QRandomGenerator::global()->generate(); } } diff --git a/tests/benchmarks/gui/graphicsview/functional/GraphicsViewBenchmark/widgets/dummydatagen.cpp b/tests/benchmarks/gui/graphicsview/functional/GraphicsViewBenchmark/widgets/dummydatagen.cpp index e3ee0f8e45..7809b38050 100644 --- a/tests/benchmarks/gui/graphicsview/functional/GraphicsViewBenchmark/widgets/dummydatagen.cpp +++ b/tests/benchmarks/gui/graphicsview/functional/GraphicsViewBenchmark/widgets/dummydatagen.cpp @@ -27,6 +27,7 @@ ****************************************************************************/ #include +#include #include "theme.h" #include "dummydatagen.h" @@ -65,12 +66,11 @@ DummyDataGenerator::~DummyDataGenerator() void DummyDataGenerator::Reset() { - qsrand(100); } QString DummyDataGenerator::randomPhoneNumber(QString indexNumber) { - int index = qrand()%m_countryCodes.count(); + int index = QRandomGenerator::global()->bounded(m_countryCodes.count()); QString countryCode = m_countryCodes.at(index); QString areaCode = QString::number(index) + QString("0").repeated(2-QString::number(index).length()); QString beginNumber = QString::number(555-index*2); @@ -84,13 +84,13 @@ QString DummyDataGenerator::randomFirstName() { m_isMale = !m_isMale; if (m_isMale) - return m_firstNamesM.at(qrand()%m_firstNamesM.count()); - return m_firstNamesF.at(qrand()%m_firstNamesF.count()); + return m_firstNamesM.at(QRandomGenerator::global()->bounded(m_firstNamesM.count())); + return m_firstNamesF.at(QRandomGenerator::global()->bounded(m_firstNamesF.count())); } QString DummyDataGenerator::randomLastName() { - return m_lastNames.at(qrand()%m_lastNames.count()); + return m_lastNames.at(QRandomGenerator::global()->bounded(m_lastNames.count())); } QString DummyDataGenerator::randomName() @@ -101,8 +101,8 @@ QString DummyDataGenerator::randomName() QString DummyDataGenerator::randomIconItem() { QString avatar = Theme::p()->pixmapPath() + "contact_default_icon.svg"; - if (qrand()%4) { - int randVal = 1+qrand()%25; + if (QRandomGenerator::global()->bounded(4)) { + int randVal = 1+QRandomGenerator::global()->bounded(25); if (m_isMale && randVal > 15) { randVal -= 15; @@ -118,7 +118,7 @@ QString DummyDataGenerator::randomIconItem() QString DummyDataGenerator::randomStatusItem() { - switch (qrand()%3) { + switch (QRandomGenerator::global()->bounded(3)) { case 0: return Theme::p()->pixmapPath() + "contact_status_online.svg"; case 1: return Theme::p()->pixmapPath() + "contact_status_offline.svg"; case 2: return Theme::p()->pixmapPath() + "contact_status_idle.svg"; diff --git a/tests/benchmarks/gui/graphicsview/qgraphicsview/benchapps/moveItems/main.cpp b/tests/benchmarks/gui/graphicsview/qgraphicsview/benchapps/moveItems/main.cpp index 6c97f94683..e0cc0f8eb4 100644 --- a/tests/benchmarks/gui/graphicsview/qgraphicsview/benchapps/moveItems/main.cpp +++ b/tests/benchmarks/gui/graphicsview/qgraphicsview/benchapps/moveItems/main.cpp @@ -82,7 +82,7 @@ int main(int argc, char *argv[]) for (int i = 0; i < atoi(argv[1]); ++i) { QGraphicsRectItem *child = scene.addRect(-5, -5, 10, 10, QPen(Qt::NoPen), QBrush(Qt::blue)); - child->setPos(-50 + qrand() % 100, -50 + qrand() % 100); + child->setPos(-50 + QRandomGenerator::global()->bounded(100), -50 + QRandomGenerator::global()->bounded(100)); child->setParentItem(item); } diff --git a/tests/benchmarks/gui/graphicsview/qgraphicswidget/tst_qgraphicswidget.cpp b/tests/benchmarks/gui/graphicsview/qgraphicswidget/tst_qgraphicswidget.cpp index c2f41a7557..d9bc7f21b6 100644 --- a/tests/benchmarks/gui/graphicsview/qgraphicswidget/tst_qgraphicswidget.cpp +++ b/tests/benchmarks/gui/graphicsview/qgraphicswidget/tst_qgraphicswidget.cpp @@ -31,6 +31,7 @@ #include #include #include +#include class tst_QGraphicsWidget : public QObject { @@ -72,7 +73,9 @@ void tst_QGraphicsWidget::move() QGraphicsView view(&scene); view.show(); QBENCHMARK { - widget->setPos(qrand(),qrand()); + // truncate the random values to 24 bits to + // avoid overflowing + widget->setPos(QRandomGenerator::global()->generate() & 0xffffff, QRandomGenerator::global()->generate() & 0xffffff); } } diff --git a/tests/benchmarks/gui/painting/qtbench/benchmarktests.h b/tests/benchmarks/gui/painting/qtbench/benchmarktests.h index e4b98336c7..42b06778f9 100644 --- a/tests/benchmarks/gui/painting/qtbench/benchmarktests.h +++ b/tests/benchmarks/gui/painting/qtbench/benchmarktests.h @@ -37,6 +37,7 @@ #include #include #include +#include class Benchmark { @@ -47,9 +48,9 @@ public: : m_size(size) { for (int i=0; i<16; ++i) { - m_colors[i] = QColor::fromRgbF((rand() % 4) / 3.0, - (rand() % 4) / 3.0, - (rand() % 4) / 3.0, + m_colors[i] = QColor::fromRgbF((QRandomGenerator::global()->bounded(4)) / 3.0, + (QRandomGenerator::global()->bounded(4)) / 3.0, + (QRandomGenerator::global()->bounded(4)) / 3.0, 1); } } @@ -142,7 +143,7 @@ public: ImageFillRectBenchmark(int size) : Benchmark(QSize(size, size)) { - int s = rand() % 24 + 8; + int s = QRandomGenerator::global()->bounded(24) + 8; m_content = QImage(s, s, QImage::Format_ARGB32_Premultiplied); QPainter p(&m_content); p.fillRect(0, 0, s, s, Qt::white); diff --git a/tests/benchmarks/network/access/qnetworkreply/tst_qnetworkreply.cpp b/tests/benchmarks/network/access/qnetworkreply/tst_qnetworkreply.cpp index dfa658df11..b2f4cbd7ba 100644 --- a/tests/benchmarks/network/access/qnetworkreply/tst_qnetworkreply.cpp +++ b/tests/benchmarks/network/access/qnetworkreply/tst_qnetworkreply.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -525,7 +526,7 @@ void tst_qnetworkreply::echoPerformance() data.resize(1024*1024*10); // 10 MB // init with garbage. needed so ssl cannot compress it in an efficient way. for (size_t i = 0; i < data.size() / sizeof(int); i++) { - int r = qrand(); + char r = char(QRandomGenerator::global()->generate()); data.data()[i*sizeof(int)] = r; } -- cgit v1.2.3