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 --- .../qtconcurrentmap/tst_qtconcurrentmap.cpp | 4 +-- tests/auto/corelib/io/largefile/tst_largefile.cpp | 4 +-- tests/auto/corelib/kernel/qobject/tst_qobject.cpp | 4 +-- .../mimetypes/qmimedatabase/tst_qmimedatabase.cpp | 1 - .../plugin/quuid/testProcessUniqueness/main.cpp | 3 --- tests/auto/corelib/thread/qfuture/tst_qfuture.cpp | 3 ++- .../corelib/tools/qalgorithms/tst_qalgorithms.cpp | 11 ++++---- .../tools/qsharedpointer/tst_qsharedpointer.cpp | 5 ++-- tests/auto/gui/image/qimage/tst_qimage.cpp | 3 ++- tests/auto/gui/painting/qpainter/tst_qpainter.cpp | 3 ++- .../gui/painting/qpathclipper/tst_qpathclipper.cpp | 5 ++-- .../text/qtextpiecetable/tst_qtextpiecetable.cpp | 19 +++++++------- tests/auto/network/access/hpack/tst_hpack.cpp | 12 ++++----- tests/auto/network/access/http2/tst_http2.cpp | 2 +- tests/auto/network/access/qftp/tst_qftp.cpp | 3 +-- .../qnetworkdiskcache/tst_qnetworkdiskcache.cpp | 14 +++++----- .../access/qnetworkreply/tst_qnetworkreply.cpp | 10 ++++---- .../network/socket/qtcpsocket/tst_qtcpsocket.cpp | 8 +++--- tests/auto/opengl/qglthreads/tst_qglthreads.cpp | 20 +++++++-------- tests/auto/other/compiler/tst_compiler.cpp | 10 ++++---- .../other/networkselftest/tst_networkselftest.cpp | 3 ++- .../dialogs/qfiledialog2/tst_qfiledialog2.cpp | 3 ++- .../qgraphicsitem/tst_qgraphicsitem.cpp | 17 ++++++------ .../qgraphicsscene/tst_qgraphicsscene.cpp | 30 +++++++++++----------- .../widgets/itemviews/qitemview/tst_qitemview.cpp | 23 +++++------------ tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp | 3 ++- .../widgets/widgets/qlineedit/tst_qlineedit.cpp | 16 +----------- .../corelib/tools/qalgorithms/tst_qalgorithms.cpp | 2 +- .../corelib/tools/qcryptographichash/main.cpp | 6 ++--- .../GraphicsViewBenchmark/widgets/dummydatagen.cpp | 16 ++++++------ .../qgraphicsview/benchapps/moveItems/main.cpp | 2 +- .../qgraphicswidget/tst_qgraphicswidget.cpp | 5 +++- .../gui/painting/qtbench/benchmarktests.h | 9 ++++--- .../access/qnetworkreply/tst_qnetworkreply.cpp | 3 ++- tests/manual/cocoa/nativewidgets/main.cpp | 2 +- .../manual/textrendering/textperformance/main.cpp | 21 +++++++-------- .../windowchildgeometry/controllerwidget.cpp | 18 +++++-------- 37 files changed, 151 insertions(+), 172 deletions(-) (limited to 'tests') diff --git a/tests/auto/concurrent/qtconcurrentmap/tst_qtconcurrentmap.cpp b/tests/auto/concurrent/qtconcurrentmap/tst_qtconcurrentmap.cpp index 96656a6dff..2d7c0c2f64 100644 --- a/tests/auto/concurrent/qtconcurrentmap/tst_qtconcurrentmap.cpp +++ b/tests/auto/concurrent/qtconcurrentmap/tst_qtconcurrentmap.cpp @@ -2204,13 +2204,13 @@ InstanceCounter slowMap(const InstanceCounter &in) InstanceCounter fastMap(const InstanceCounter &in) { - QTest::qSleep(rand() % 2 + 1); + QTest::qSleep(QRandomGenerator::global()->bounded(2) + 1); return in; } void slowReduce(int &result, const InstanceCounter&) { - QTest::qSleep(rand() % 4 + 1); + QTest::qSleep(QRandomGenerator::global()->bounded(4) + 1); ++result; } diff --git a/tests/auto/corelib/io/largefile/tst_largefile.cpp b/tests/auto/corelib/io/largefile/tst_largefile.cpp index 5975303ca6..2d13e6166d 100644 --- a/tests/auto/corelib/io/largefile/tst_largefile.cpp +++ b/tests/auto/corelib/io/largefile/tst_largefile.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #include #include @@ -174,8 +175,7 @@ static inline QByteArray generateDataBlock(int blockSize, QString text, qint64 u static qint64 counter = 0; - qint64 randomBits = ((qint64)qrand() << 32) - | ((qint64)qrand() & 0x00000000ffffffff); + qint64 randomBits = QRandomGenerator::global()->generate64(); appendRaw(block, randomBits); appendRaw(block, userBits); diff --git a/tests/auto/corelib/kernel/qobject/tst_qobject.cpp b/tests/auto/corelib/kernel/qobject/tst_qobject.cpp index 3a52c684d0..5ecdd92228 100644 --- a/tests/auto/corelib/kernel/qobject/tst_qobject.cpp +++ b/tests/auto/corelib/kernel/qobject/tst_qobject.cpp @@ -2362,8 +2362,8 @@ void tst_QObject::testUserData() // Randomize the table a bit for (int i=0; i<100; ++i) { - int p1 = rand() % USER_DATA_COUNT; - int p2 = rand() % USER_DATA_COUNT; + int p1 = QRandomGenerator::global()->bounded(USER_DATA_COUNT); + int p2 = QRandomGenerator::global()->bounded(USER_DATA_COUNT); int tmp = user_data_ids[p1]; user_data_ids[p1] = user_data_ids[p2]; diff --git a/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp b/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp index 8a46bc1c55..8883b6360f 100644 --- a/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp +++ b/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp @@ -115,7 +115,6 @@ Q_CONSTRUCTOR_FUNCTION(initializeLang) static QString seedAndTemplate() { - qsrand(QDateTime::currentSecsSinceEpoch()); return QDir::tempPath() + "/tst_qmimedatabase-XXXXXX"; } diff --git a/tests/auto/corelib/plugin/quuid/testProcessUniqueness/main.cpp b/tests/auto/corelib/plugin/quuid/testProcessUniqueness/main.cpp index daf9b1579a..d1e138d8eb 100644 --- a/tests/auto/corelib/plugin/quuid/testProcessUniqueness/main.cpp +++ b/tests/auto/corelib/plugin/quuid/testProcessUniqueness/main.cpp @@ -35,9 +35,6 @@ int main(int argc, char **argv) Q_UNUSED(argc) Q_UNUSED(argv) - // First, break QUuid. - qrand(); - // Now print a few uuids. printf("%s", qPrintable(QUuid::createUuid().toString())); printf("%s", qPrintable(QUuid::createUuid().toString())); diff --git a/tests/auto/corelib/thread/qfuture/tst_qfuture.cpp b/tests/auto/corelib/thread/qfuture/tst_qfuture.cpp index 37b052bf1d..58bebe19ac 100644 --- a/tests/auto/corelib/thread/qfuture/tst_qfuture.cpp +++ b/tests/auto/corelib/thread/qfuture/tst_qfuture.cpp @@ -36,6 +36,7 @@ #include #include #include +#include #include // COM interface macro. @@ -1456,7 +1457,7 @@ void tst_QFuture::nonGlobalThreadPool() void run() Q_DECL_OVERRIDE { - const int ms = 100 + (qrand() % 100 - 100/2); + const int ms = 100 + (QRandomGenerator::global()->bounded(100) - 100/2); QThread::msleep(ms); reportResult(Answer); reportFinished(); diff --git a/tests/auto/corelib/tools/qalgorithms/tst_qalgorithms.cpp b/tests/auto/corelib/tools/qalgorithms/tst_qalgorithms.cpp index e13c2894af..72299402f0 100644 --- a/tests/auto/corelib/tools/qalgorithms/tst_qalgorithms.cpp +++ b/tests/auto/corelib/tools/qalgorithms/tst_qalgorithms.cpp @@ -37,6 +37,7 @@ #include #include #include +#include #include #define Q_TEST_PERFORMANCE 0 @@ -133,7 +134,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); @@ -1082,12 +1083,12 @@ void tst_QAlgorithms::popCount_data_impl(size_t sizeof_T_Int) // and some random ones: if (sizeof_T_Int >= 8) for (size_t i = 0; i < 1000; ++i) { - const quint64 input = quint64(qrand()) << 32 | quint32(qrand()); + const quint64 input = QRandomGenerator::global()->generate64(); QTest::addRow("0x%016llx", input) << input << bitsSetInInt64(input); } else if (sizeof_T_Int >= 2) for (size_t i = 0; i < 1000 ; ++i) { - const quint32 input = qrand(); + const quint32 input = QRandomGenerator::global()->generate(); if (sizeof_T_Int >= 4) QTest::addRow("0x%08x", input) << quint64(input) << bitsSetInInt(input); else @@ -1129,7 +1130,7 @@ void tst_QAlgorithms::countTrailing_data_impl(size_t sizeof_T_Int) // and some random ones: for (uint i = 0; i < sizeof_T_Int*8; ++i) { for (uint j = 0; j < sizeof_T_Int*3; ++j) { // 3 is arbitrary - const quint64 r = quint64(qrand()) << 32 | quint32(qrand()); + const quint64 r = QRandomGenerator::global()->generate64(); const quint64 b = Q_UINT64_C(1) << i; const quint64 mask = ((~(b-1)) ^ b) & type_mask; const quint64 input = (r&mask) | b; @@ -1166,7 +1167,7 @@ void tst_QAlgorithms::countLeading_data_impl(size_t sizeof_T_Int) // and some random ones: for (uint i = 0; i < sizeof_T_Int*8; ++i) { for (uint j = 0; j < sizeof_T_Int*3; ++j) { // 3 is arbitrary - const quint64 r = quint64(qrand()) << 32 | quint32(qrand()); + const quint64 r = QRandomGenerator::global()->generate64(); const quint64 b = Q_UINT64_C(1) << i; const quint64 mask = b-1; const quint64 input = (r&mask) | b; diff --git a/tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp b/tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp index 442d4d089c..e1dcdb8407 100644 --- a/tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp +++ b/tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp @@ -1884,7 +1884,7 @@ class StrongThread: public QThread protected: void run() { - usleep(rand() % 2000); + usleep(QRandomGenerator::global()->bounded(2000)); ptr->ref(); ptr.clear(); } @@ -1897,7 +1897,7 @@ class WeakThread: public QThread protected: void run() { - usleep(rand() % 2000); + usleep(QRandomGenerator::global()->bounded(2000)); QSharedPointer ptr = weak; if (ptr) ptr->ref(); @@ -1959,7 +1959,6 @@ void tst_QSharedPointer::threadStressTest() base.clear(); - srand(time(NULL)); // start threads for (int i = 0; i < allThreads.count(); ++i) if (allThreads[i]) allThreads[i]->start(); diff --git a/tests/auto/gui/image/qimage/tst_qimage.cpp b/tests/auto/gui/image/qimage/tst_qimage.cpp index 235d53f3c1..54eb8ab99c 100644 --- a/tests/auto/gui/image/qimage/tst_qimage.cpp +++ b/tests/auto/gui/image/qimage/tst_qimage.cpp @@ -33,6 +33,7 @@ #include #include #include +#include #include #include @@ -1754,7 +1755,7 @@ void tst_QImage::smoothScale2() static inline int rand8() { - return int(256. * (qrand() / (RAND_MAX + 1.0))); + return QRandomGenerator::global()->bounded(256); } void tst_QImage::smoothScale3_data() diff --git a/tests/auto/gui/painting/qpainter/tst_qpainter.cpp b/tests/auto/gui/painting/qpainter/tst_qpainter.cpp index 779783d5ec..b8243a2b54 100644 --- a/tests/auto/gui/painting/qpainter/tst_qpainter.cpp +++ b/tests/auto/gui/painting/qpainter/tst_qpainter.cpp @@ -45,6 +45,7 @@ #include #endif #include +#include #include #include @@ -3052,7 +3053,7 @@ void tst_QPainter::fpe_steepSlopes_data() qreal randf() { - return rand() / (RAND_MAX + 1.0); + return QRandomGenerator::global()->bounded(1.0); } QPointF randInRect(const QRectF &rect) diff --git a/tests/auto/gui/painting/qpathclipper/tst_qpathclipper.cpp b/tests/auto/gui/painting/qpathclipper/tst_qpathclipper.cpp index 14ba9c5c84..93035af7d3 100644 --- a/tests/auto/gui/painting/qpathclipper/tst_qpathclipper.cpp +++ b/tests/auto/gui/painting/qpathclipper/tst_qpathclipper.cpp @@ -35,6 +35,7 @@ #include #include #include +#include #include @@ -423,8 +424,8 @@ void tst_QPathClipper::clip() static inline QPointF randomPointInRect(const QRectF &rect) { - qreal rx = qrand() / (RAND_MAX + 1.); - qreal ry = qrand() / (RAND_MAX + 1.); + qreal rx = QRandomGenerator::global()->bounded(1.0); + qreal ry = QRandomGenerator::global()->bounded(1.0); return QPointF(rect.left() + rx * rect.width(), rect.top() + ry * rect.height()); diff --git a/tests/auto/gui/text/qtextpiecetable/tst_qtextpiecetable.cpp b/tests/auto/gui/text/qtextpiecetable/tst_qtextpiecetable.cpp index ca01e83dbe..39f5e9ecc3 100644 --- a/tests/auto/gui/text/qtextpiecetable/tst_qtextpiecetable.cpp +++ b/tests/auto/gui/text/qtextpiecetable/tst_qtextpiecetable.cpp @@ -145,7 +145,7 @@ void tst_QTextPieceTable::insertion3() { QString compare; for (int i = 0; i < 20000; ++i) { - int pos = rand() % (i+1); + int pos = QRandomGenerator::global()->bounded(i+1); QChar c((unsigned short)(i & 0xff) + 1); QString str; str += c; @@ -159,7 +159,7 @@ void tst_QTextPieceTable::insertion4() { QString compare; for (int i = 0; i < 20000; ++i) { - int pos = rand() % (i+1); + int pos = QRandomGenerator::global()->generate() % (i+1); QChar c((unsigned short)((i % 26) + (i>25?'A':'a'))); QString str; str += c; @@ -178,7 +178,7 @@ void tst_QTextPieceTable::insertion5() { QString compare; for (int i = 0; i < 20000; ++i) { - int pos = rand() % (i+1); + int pos = QRandomGenerator::global()->generate() % (i+1); QChar c((unsigned short)((i % 26) + (i>25?'A':'a'))); QString str; str += c; @@ -236,8 +236,8 @@ void tst_QTextPieceTable::removal3() QString compare; int l = 0; for (int i = 0; i < 20000; ++i) { - bool remove = l && (rand() % 2); - int pos = rand() % (remove ? l : (l+1)); + bool remove = l && (QRandomGenerator::global()->bounded(2)); + int pos = QRandomGenerator::global()->bounded(remove ? l : (l+1)); QChar c((unsigned short)((i % 26) + (i>25?'A':'a'))); QString str; str += c; @@ -263,8 +263,8 @@ void tst_QTextPieceTable::removal4() QString compare; int l = 0; for (int i = 0; i < 20000; ++i) { - bool remove = l && (rand() % 2); - int pos = (l > 1) ? rand() % (remove ? l-1 : l) : 0; + bool remove = l && (QRandomGenerator::global()->bounded(2)); + int pos = (l > 1) ? QRandomGenerator::global()->bounded(remove ? l-1 : l) : 0; QChar c((unsigned short)((i % 26) + (i>25?'A':'a'))); QString str; if (c != 'a') { @@ -472,13 +472,12 @@ void tst_QTextPieceTable::undoRedo10() void tst_QTextPieceTable::undoRedo11() { - srand(3); const int loops = 20; QString compare; int l = 0; for (int i = 0; i < loops; ++i) { - bool remove = l && (rand() % 2); - int pos = (l > 1) ? rand() % (remove ? l-1 : l) : 0; + bool remove = l && (QRandomGenerator::global()->bounded(2)); + int pos = (l > 1) ? QRandomGenerator::global()->bounded(remove ? l-1 : l) : 0; QChar c((unsigned short)((i % 26) + (i>25?'A':'a'))); QString str; str += c; diff --git a/tests/auto/network/access/hpack/tst_hpack.cpp b/tests/auto/network/access/hpack/tst_hpack.cpp index bd337c9f5f..810745a065 100644 --- a/tests/auto/network/access/hpack/tst_hpack.cpp +++ b/tests/auto/network/access/hpack/tst_hpack.cpp @@ -273,13 +273,13 @@ void tst_Hpack::bitstreamCompression() std::vector buffer; BitOStream out(buffer); for (unsigned i = 0; i < nValues; ++i) { - const bool isString = std::rand() % 1000 > 500; + const bool isString = QRandomGenerator::global()->bounded(1000) > 500; isA.push_back(isString); if (!isString) { - integers.push_back(std::rand() % 1000); + integers.push_back(QRandomGenerator::global()->bounded(1000u)); out.write(integers.back()); } else { - const auto start = std::rand() % (bytes.length() / 2); + const auto start = QRandomGenerator::global()->bounded(uint(bytes.length()) / 2); auto end = start * 2; if (!end) end = bytes.length() / 2; @@ -287,7 +287,7 @@ void tst_Hpack::bitstreamCompression() const auto &s = strings.back(); totalStringBytes += s.size(); QByteArray data(s.c_str(), int(s.size())); - const bool compressed(std::rand() % 1000 > 500); + const bool compressed(QRandomGenerator::global()->bounded(1000) > 500); out.write(data, compressed); } } @@ -442,8 +442,8 @@ void tst_Hpack::lookupTableDynamic() // Strings are repeating way too often, I want to // have at least some items really evicted and not found, // therefore these weird dances with start/len. - const quint32 start = std::rand() % (dataSize - 10); - quint32 len = std::rand() % (dataSize - start); + const quint32 start = QRandomGenerator::global()->bounded(dataSize - 10); + quint32 len = QRandomGenerator::global()->bounded(dataSize - start); if (!len) len = 1; diff --git a/tests/auto/network/access/http2/tst_http2.cpp b/tests/auto/network/access/http2/tst_http2.cpp index 7b453ca635..51e1849512 100644 --- a/tests/auto/network/access/http2/tst_http2.cpp +++ b/tests/auto/network/access/http2/tst_http2.cpp @@ -224,7 +224,7 @@ void tst_Http2::multipleRequests() }; for (int i = 0; i < nRequests; ++i) - sendRequest(i, priorities[std::rand() % 3]); + sendRequest(i, priorities[QRandomGenerator::global()->bounded(3)]); runEventLoop(); diff --git a/tests/auto/network/access/qftp/tst_qftp.cpp b/tests/auto/network/access/qftp/tst_qftp.cpp index fba0508f04..4bc43f068c 100644 --- a/tests/auto/network/access/qftp/tst_qftp.cpp +++ b/tests/auto/network/access/qftp/tst_qftp.cpp @@ -276,8 +276,7 @@ void tst_QFtp::init() inFileDirExistsFunction = false; - srand(time(0)); - uniqueExtension = QString::number((quintptr)this) + QString::number(rand()) + uniqueExtension = QString::number((quintptr)this) + QString::number(QRandomGenerator::global()->generate()) + QString::number((qulonglong)time(0)); } diff --git a/tests/auto/network/access/qnetworkdiskcache/tst_qnetworkdiskcache.cpp b/tests/auto/network/access/qnetworkdiskcache/tst_qnetworkdiskcache.cpp index e996347a9a..856033fb63 100644 --- a/tests/auto/network/access/qnetworkdiskcache/tst_qnetworkdiskcache.cpp +++ b/tests/auto/network/access/qnetworkdiskcache/tst_qnetworkdiskcache.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include @@ -693,25 +694,25 @@ public: if (write) { QNetworkCacheMetaData m; - if (qrand() % 2 == 0) + if (QRandomGenerator::global()->bounded(2) == 0) m = metaData; else m = metaData2; - if (qrand() % 20 == 1) { + if (QRandomGenerator::global()->bounded(20) == 1) { //qDebug() << "write update"; cache.updateMetaData(m); continue; } QIODevice *device = cache.prepare(m); - if (qrand() % 20 == 1) { + if (QRandomGenerator::global()->bounded(20) == 1) { //qDebug() << "write remove"; cache.remove(url); continue; } QVERIFY(device); - if (qrand() % 2 == 0) + if (QRandomGenerator::global()->bounded(2) == 0) device->write(longString); else device->write(longString2); @@ -740,9 +741,9 @@ public: delete d; } } - if (qrand() % 5 == 1) + if (QRandomGenerator::global()->bounded(5) == 1) cache.remove(url); - if (qrand() % 5 == 1) + if (QRandomGenerator::global()->bounded(5) == 1) cache.clear(); sleep(0); } @@ -791,7 +792,6 @@ void tst_QNetworkDiskCache::sync() return; QTime midnight(0, 0, 0); - qsrand(midnight.secsTo(QTime::currentTime())); Runner reader(tempDir.path()); reader.dt = QDateTime::currentDateTime(); reader.write = false; diff --git a/tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp b/tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp index 93afca3d48..b6b5f5ae70 100644 --- a/tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp +++ b/tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -120,12 +121,11 @@ class tst_QNetworkReply: public QObject static QString createUniqueExtension() { if (!seedCreated) { - qsrand(QTime(0,0,0).msecsTo(QTime::currentTime()) + QCoreApplication::applicationPid()); seedCreated = true; // not thread-safe, but who cares } return QString::number(QTime(0, 0, 0).msecsTo(QTime::currentTime())) + QLatin1Char('-') + QString::number(QCoreApplication::applicationPid()) - + QLatin1Char('-') + QString::number(qrand()); + + QLatin1Char('-') + QString::number(QRandomGenerator::global()->generate()); } static QString tempRedirectReplyStr() { @@ -4852,7 +4852,7 @@ void tst_QNetworkReply::ioPostToHttpsUploadProgress() // server send the data much faster than expected. // So better provide random data that cannot be compressed. for (int i = 0; i < wantedSize; ++i) - sourceFile += (char)qrand(); + sourceFile += (char)QRandomGenerator::global()->generate(); // emulate a minimal https server SslServer server; @@ -4932,7 +4932,7 @@ void tst_QNetworkReply::ioGetFromBuiltinHttp() // server send the data much faster than expected. // So better provide random data that cannot be compressed. for (int i = 0; i < wantedSize; ++i) - testData += (char)qrand(); + testData += (char)QRandomGenerator::global()->generate(); QByteArray httpResponse = QByteArray("HTTP/1.0 200 OK\r\nContent-Length: "); httpResponse += QByteArray::number(testData.size()); @@ -8748,7 +8748,7 @@ public slots: m_receivedData += data; if (!m_parsedHeaders && m_receivedData.contains("\r\n\r\n")) { m_parsedHeaders = true; - QTimer::singleShot(qrand()%60, this, SLOT(closeDelayed())); // simulate random network latency + QTimer::singleShot(QRandomGenerator::global()->bounded(60), this, SLOT(closeDelayed())); // simulate random network latency // This server simulates a web server connection closing, e.g. because of Apaches MaxKeepAliveRequests or KeepAliveTimeout // In this case QNAM needs to re-send the upload data but it had a bug which then corrupts the upload // This test catches that. diff --git a/tests/auto/network/socket/qtcpsocket/tst_qtcpsocket.cpp b/tests/auto/network/socket/qtcpsocket/tst_qtcpsocket.cpp index 7340817ade..d7c8c8c378 100644 --- a/tests/auto/network/socket/qtcpsocket/tst_qtcpsocket.cpp +++ b/tests/auto/network/socket/qtcpsocket/tst_qtcpsocket.cpp @@ -1,7 +1,7 @@ /**************************************************************************** ** ** Copyright (C) 2016 The Qt Company Ltd. -** Copyright (C) 2016 Intel Corporation. +** Copyright (C) 2017 Intel Corporation. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the test suite of the Qt Toolkit. @@ -57,6 +57,7 @@ #if QT_CONFIG(process) # include #endif +#include #include #include #include @@ -305,8 +306,6 @@ public: tst_QTcpSocket::tst_QTcpSocket() : firstFailName("qt-test-server-first-fail") { - qsrand(time(NULL)); - tmpSocket = 0; //This code relates to the socketsConstructedBeforeEventLoop test case @@ -581,8 +580,7 @@ void tst_QTcpSocket::bind() // try to get a random port number // we do this to ensure we're not trying to bind to the same port as we've just used in // a previous run - race condition with the OS actually freeing the port - Q_STATIC_ASSERT(RAND_MAX > 1024); - port = qrand() & USHRT_MAX; + port = QRandomGenerator::global()->generate() & USHRT_MAX; if (port < 1024) continue; } diff --git a/tests/auto/opengl/qglthreads/tst_qglthreads.cpp b/tests/auto/opengl/qglthreads/tst_qglthreads.cpp index 1a7ccba9be..90fc4e0f2a 100644 --- a/tests/auto/opengl/qglthreads/tst_qglthreads.cpp +++ b/tests/auto/opengl/qglthreads/tst_qglthreads.cpp @@ -138,7 +138,7 @@ public: makeCurrent(); QPainter p(this); - p.fillRect(rect(), QColor(rand() % 256, rand() % 256, rand() % 256)); + p.fillRect(rect(), QColor(QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256))); p.setPen(Qt::red); p.setFont(QFont("SansSerif", 24)); p.drawText(rect(), Qt::AlignCenter, "This is an autotest"); @@ -229,7 +229,7 @@ public: int height = 300; QImage image(width, height, QImage::Format_RGB32); QPainter p(&image); - p.fillRect(image.rect(), QColor(rand() % 256, rand() % 256, rand() % 256)); + p.fillRect(image.rect(), QColor(QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256))); p.setPen(Qt::red); p.setFont(QFont("SansSerif", 24)); p.drawText(image.rect(), Qt::AlignCenter, "This is an autotest"); @@ -271,7 +271,7 @@ public: public slots: void receiveImage(const QImage &image) { m_images << image; - m_positions << QPoint(-rand() % width() / 2, -rand() % height() / 2); + m_positions << QPoint(-QRandomGenerator::global()->bounded(width() / 2), -QRandomGenerator::global()->bounded(height() / 2)); m_semaphore->release(1); @@ -326,7 +326,7 @@ void tst_QGLThreads::textureUploadInThread() if that works, we're in good shape.. */ -static inline float qrandom() { return (rand() % 100) / 100.f; } +static inline float qrandom() { return (QRandomGenerator::global()->bounded(100)) / 100.f; } void renderAScene(int w, int h) { @@ -345,12 +345,12 @@ void renderAScene(int w, int h) for (int i=0; i<1000; ++i) { GLfloat pos[] = { - (rand() % 100) / 100.f, - (rand() % 100) / 100.f, - (rand() % 100) / 100.f, - (rand() % 100) / 100.f, - (rand() % 100) / 100.f, - (rand() % 100) / 100.f + (QRandomGenerator::global()->bounded(100)) / 100.f, + (QRandomGenerator::global()->bounded(100)) / 100.f, + (QRandomGenerator::global()->bounded(100)) / 100.f, + (QRandomGenerator::global()->bounded(100)) / 100.f, + (QRandomGenerator::global()->bounded(100)) / 100.f, + (QRandomGenerator::global()->bounded(100)) / 100.f }; funcs->glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, pos); diff --git a/tests/auto/other/compiler/tst_compiler.cpp b/tests/auto/other/compiler/tst_compiler.cpp index 121d731757..8b59f2758f 100644 --- a/tests/auto/other/compiler/tst_compiler.cpp +++ b/tests/auto/other/compiler/tst_compiler.cpp @@ -1,7 +1,7 @@ /**************************************************************************** ** ** Copyright (C) 2016 The Qt Company Ltd. -** Copyright (C) 2016 Intel Corporation. +** Copyright (C) 2017 Intel Corporation. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the test suite of the Qt Toolkit. @@ -802,7 +802,7 @@ void tst_Compiler::cxx11_auto_type() QSKIP("Compiler does not support C++11 feature"); #else auto i = 1; - auto x = qrand(); + auto x = QRandomGenerator::global()->generate(); auto l = 1L; auto s = QStringLiteral("Hello World"); @@ -851,8 +851,8 @@ void tst_Compiler::cxx11_decltype() #ifndef Q_COMPILER_DECLTYPE QSKIP("Compiler does not support C++11 feature"); #else - decltype(qrand()) i = 0; - QCOMPARE(i, 0); + decltype(QRandomGenerator::global()->generate()) i = 0; + QCOMPARE(i, 0U); #endif } @@ -1549,7 +1549,7 @@ void tst_Compiler::runtimeArrays() #if __cpp_runtime_arrays-0 < 201304 QSKIP("Compiler does not support this C++14 feature"); #else - int i[qrand() & 0x1f]; + int i[QRandomGenerator::global()->generate() & 0x1f]; Q_UNUSED(i); #endif } diff --git a/tests/auto/other/networkselftest/tst_networkselftest.cpp b/tests/auto/other/networkselftest/tst_networkselftest.cpp index 3b696604b5..dc353d2090 100644 --- a/tests/auto/other/networkselftest/tst_networkselftest.cpp +++ b/tests/auto/other/networkselftest/tst_networkselftest.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include #include @@ -538,7 +539,7 @@ void tst_NetworkSelfTest::imapServer() void tst_NetworkSelfTest::httpServer() { QByteArray uniqueExtension = QByteArray::number((qulonglong)this) + - QByteArray::number((qulonglong)qrand()) + + QByteArray::number((qulonglong)QRandomGenerator::global()->generate()) + QByteArray::number(QDateTime::currentSecsSinceEpoch()); netChat(80, QList() diff --git a/tests/auto/widgets/dialogs/qfiledialog2/tst_qfiledialog2.cpp b/tests/auto/widgets/dialogs/qfiledialog2/tst_qfiledialog2.cpp index 09e070ac20..eafd4d7cea 100644 --- a/tests/auto/widgets/dialogs/qfiledialog2/tst_qfiledialog2.cpp +++ b/tests/auto/widgets/dialogs/qfiledialog2/tst_qfiledialog2.cpp @@ -48,6 +48,7 @@ #include #include #include +#include #include "../../../../../src/widgets/dialogs/qsidebar_p.h" #include "../../../../../src/widgets/dialogs/qfilesystemmodel_p.h" #include "../../../../../src/widgets/dialogs/qfiledialog_p.h" @@ -1196,7 +1197,7 @@ void tst_QFileDialog2::QTBUG6558_showDirsOnly() { const QString tempPath = tempDir.path(); QDir dirTemp(tempPath); - const QString tempName = QLatin1String("showDirsOnly.") + QString::number(qrand()); + const QString tempName = QLatin1String("showDirsOnly.") + QString::number(QRandomGenerator::global()->generate()); dirTemp.mkdir(tempName); dirTemp.cd(tempName); QTRY_VERIFY(dirTemp.exists()); diff --git a/tests/auto/widgets/graphicsview/qgraphicsitem/tst_qgraphicsitem.cpp b/tests/auto/widgets/graphicsview/qgraphicsitem/tst_qgraphicsitem.cpp index 2abb6e3515..da15583d5d 100644 --- a/tests/auto/widgets/graphicsview/qgraphicsitem/tst_qgraphicsitem.cpp +++ b/tests/auto/widgets/graphicsview/qgraphicsitem/tst_qgraphicsitem.cpp @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -3595,12 +3596,12 @@ void tst_QGraphicsItem::group() QList newItems; for (int i = 0; i < 100; ++i) { QGraphicsItem *item = scene.addRect(QRectF(-25, -25, 50, 50), QPen(Qt::black, 0), - QBrush(QColor(rand() % 255, rand() % 255, - rand() % 255, rand() % 255))); + QBrush(QColor(QRandomGenerator::global()->bounded(255), QRandomGenerator::global()->bounded(255), + QRandomGenerator::global()->bounded(255), QRandomGenerator::global()->bounded(255)))); newItems << item; - item->setPos(-1000 + rand() % 2000, - -1000 + rand() % 2000); - item->setTransform(QTransform().rotate(rand() % 90), true); + item->setPos(-1000 + QRandomGenerator::global()->bounded(2000), + -1000 + QRandomGenerator::global()->bounded(2000)); + item->setTransform(QTransform().rotate(QRandomGenerator::global()->bounded(90)), true); } view.fitInView(scene.itemsBoundingRect()); @@ -4143,8 +4144,8 @@ void tst_QGraphicsItem::ensureVisible() for (int x = -100; x < 100; x += 25) { for (int y = -100; y < 100; y += 25) { - int xmargin = rand() % 75; - int ymargin = rand() % 75; + int xmargin = QRandomGenerator::global()->bounded(75); + int ymargin = QRandomGenerator::global()->bounded(75); item->ensureVisible(x, y, 25, 25, xmargin, ymargin); QApplication::processEvents(); @@ -7137,7 +7138,7 @@ public: : QGraphicsRectItem(QRectF(-10, -10, 20, 20)) { setPen(QPen(Qt::black, 0)); - setBrush(QColor(qrand() % 256, qrand() % 256, qrand() % 256)); + setBrush(QColor(QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256))); } QTransform x; diff --git a/tests/auto/widgets/graphicsview/qgraphicsscene/tst_qgraphicsscene.cpp b/tests/auto/widgets/graphicsview/qgraphicsscene/tst_qgraphicsscene.cpp index 7615c5e821..968233438a 100644 --- a/tests/auto/widgets/graphicsview/qgraphicsscene/tst_qgraphicsscene.cpp +++ b/tests/auto/widgets/graphicsview/qgraphicsscene/tst_qgraphicsscene.cpp @@ -1507,10 +1507,10 @@ void tst_QGraphicsScene::mouseGrabberItem() QCOMPARE(scene.mouseGrabberItem(), topMostItem); // Geometrical changes should not affect the mouse grabber. - item->setZValue(rand() % 500); - item2->setZValue(rand() % 500); - item->setPos(rand() % 50000, rand() % 50000); - item2->setPos(rand() % 50000, rand() % 50000); + item->setZValue(QRandomGenerator::global()->bounded(500)); + item2->setZValue(QRandomGenerator::global()->bounded(500)); + item->setPos(QRandomGenerator::global()->bounded(50000), QRandomGenerator::global()->bounded(50000)); + item2->setPos(QRandomGenerator::global()->bounded(50000), QRandomGenerator::global()->bounded(50000)); } QGraphicsSceneMouseEvent releaseEvent(QEvent::GraphicsSceneMouseRelease); @@ -3424,7 +3424,7 @@ void tst_QGraphicsScene::task139710_bspTreeCrash() // add 1000 more items - the BSP tree is now resized for (int i = 0; i < 1000; ++i) { QGraphicsRectItem *item = scene.addRect(QRectF(0, 0, 200, 200)); - item->setPos(qrand() % 10000, qrand() % 10000); + item->setPos(QRandomGenerator::global()->bounded(10000), QRandomGenerator::global()->bounded(10000)); } // trigger delayed item indexing for the first 1000 items @@ -3433,7 +3433,7 @@ void tst_QGraphicsScene::task139710_bspTreeCrash() // add 1000 more items - the BSP tree is now resized for (int i = 0; i < 1000; ++i) { QGraphicsRectItem *item = scene.addRect(QRectF(0, 0, 200, 200)); - item->setPos(qrand() % 10000, qrand() % 10000); + item->setPos(QRandomGenerator::global()->bounded(10000), QRandomGenerator::global()->bounded(10000)); } // get items from the BSP tree and use them. there was junk in the tree @@ -3543,15 +3543,15 @@ void tst_QGraphicsScene::sorting() QGraphicsRectItem *c_2_1 = new QGraphicsRectItem(0, 0, 30, 30, c_2); QGraphicsRectItem *c_2_1_1 = new QGraphicsRectItem(0, 0, 20, 20, c_2_1); QGraphicsRectItem *c_2_2 = new QGraphicsRectItem(0, 0, 30, 30, c_2); - t_1->setBrush(QColor(qrand() % 256, qrand() % 256, qrand() % 256)); - c_1->setBrush(QColor(qrand() % 256, qrand() % 256, qrand() % 256)); - c_1_1->setBrush(QColor(qrand() % 256, qrand() % 256, qrand() % 256)); - c_1_1_1->setBrush(QColor(qrand() % 256, qrand() % 256, qrand() % 256)); - c_1_2->setBrush(QColor(qrand() % 256, qrand() % 256, qrand() % 256)); - c_2->setBrush(QColor(qrand() % 256, qrand() % 256, qrand() % 256)); - c_2_1->setBrush(QColor(qrand() % 256, qrand() % 256, qrand() % 256)); - c_2_1_1->setBrush(QColor(qrand() % 256, qrand() % 256, qrand() % 256)); - c_2_2->setBrush(QColor(qrand() % 256, qrand() % 256, qrand() % 256)); + t_1->setBrush(QColor(QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256))); + c_1->setBrush(QColor(QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256))); + c_1_1->setBrush(QColor(QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256))); + c_1_1_1->setBrush(QColor(QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256))); + c_1_2->setBrush(QColor(QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256))); + c_2->setBrush(QColor(QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256))); + c_2_1->setBrush(QColor(QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256))); + c_2_1_1->setBrush(QColor(QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256))); + c_2_2->setBrush(QColor(QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256))); c_1->setPos(23, 18); c_1_1->setPos(24, 28); diff --git a/tests/auto/widgets/itemviews/qitemview/tst_qitemview.cpp b/tests/auto/widgets/itemviews/qitemview/tst_qitemview.cpp index 1fd7b31e22..f1239b2e7c 100644 --- a/tests/auto/widgets/itemviews/qitemview/tst_qitemview.cpp +++ b/tests/auto/widgets/itemviews/qitemview/tst_qitemview.cpp @@ -30,16 +30,6 @@ #include #include #include "viewstotest.cpp" -#include - -#if defined(Q_OS_UNIX) || defined(Q_OS_WIN) -#include -#endif - -#if defined(Q_OS_WIN) -# define random rand -# define srandom srand -#endif /*! See viewstotest.cpp for instructions on how to have your view tested with these tests. @@ -410,13 +400,15 @@ void touch(QWidget *widget, Qt::KeyboardModifier modifier, Qt::Key keyPress){ int width = widget->width(); int height = widget->height(); for (int i = 0; i < 5; ++i) { - QTest::mouseClick(widget, Qt::LeftButton, modifier, QPoint(random() % width, random() % height)); - QTest::mouseDClick(widget, Qt::LeftButton, modifier, QPoint(random() % width, random() % height)); - QPoint press(random() % width, random() % height); - QPoint releasePoint(random() % width, random() % height); + QTest::mouseClick(widget, Qt::LeftButton, modifier, + QPoint(QRandomGenerator::global()->bounded(width), QRandomGenerator::global()->bounded(height))); + QTest::mouseDClick(widget, Qt::LeftButton, modifier, + QPoint(QRandomGenerator::global()->bounded(width), QRandomGenerator::global()->bounded(height))); + QPoint press(QRandomGenerator::global()->bounded(width), QRandomGenerator::global()->bounded(height)); + QPoint releasePoint(QRandomGenerator::global()->bounded(width), QRandomGenerator::global()->bounded(height)); QTest::mousePress(widget, Qt::LeftButton, modifier, press); QTest::mouseMove(widget, releasePoint); - if (random() % 1 == 0) + if (QRandomGenerator::global()->bounded(1) == 0) QTest::mouseRelease(widget, Qt::LeftButton, 0, releasePoint); else QTest::mouseRelease(widget, Qt::LeftButton, modifier, releasePoint); @@ -443,7 +435,6 @@ void tst_QItemView::spider() view->setModel(treeModel); view->show(); QVERIFY(QTest::qWaitForWindowActive(view)); - srandom(time(0)); touch(view->viewport(), Qt::NoModifier, Qt::Key_Left); touch(view->viewport(), Qt::ShiftModifier, Qt::Key_Enter); touch(view->viewport(), Qt::ControlModifier, Qt::Key_Backspace); diff --git a/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp b/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp index 30fc927f7d..7d4024b556 100644 --- a/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp +++ b/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp @@ -50,6 +50,7 @@ #include #include #include +#include #include #include #include @@ -9826,7 +9827,7 @@ void tst_QWidget::grab() for (int row = 0; row < image.height(); ++row) { QRgb *line = reinterpret_cast(image.scanLine(row)); for (int col = 0; col < image.width(); ++col) - line[col] = qRgba(rand() & 255, row, col, opaque ? 255 : 127); + line[col] = qRgba(QRandomGenerator::global()->bounded(255), row, col, opaque ? 255 : 127); } QPalette pal = widget.palette(); diff --git a/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp b/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp index 71969ec695..3d9620e75a 100644 --- a/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp +++ b/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp @@ -43,10 +43,6 @@ #include #include "qclipboard.h" -#ifdef Q_OS_MAC -#include // For the random function. -#endif - #include #include #include @@ -1949,17 +1945,7 @@ void tst_QLineEdit::noCursorBlinkWhenReadOnly() static void figureOutProperKey(Qt::Key &key, Qt::KeyboardModifiers &pressState) { #ifdef Q_OS_MAC - static bool tst_lineedit_randomized = false; - // Mac has 3 different ways of accomplishing this (same for moving to the back) - // So I guess we should just randomly do this for now. Which may get people mad, but if - // we fail at one point, it's just a matter of setting roll to the correct value - // instead of random. - - if (!tst_lineedit_randomized) { - tst_lineedit_randomized = true; - ::srandom(ulong(time(0))); - } - long roll = ::random() % 3; + long roll = QRandomGenerator::global()->bounded(3); switch (roll) { case 0: key = key == Qt::Key_Home ? Qt::Key_Up : Qt::Key_Down; 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; } diff --git a/tests/manual/cocoa/nativewidgets/main.cpp b/tests/manual/cocoa/nativewidgets/main.cpp index bfa97aa62f..3923e30de7 100644 --- a/tests/manual/cocoa/nativewidgets/main.cpp +++ b/tests/manual/cocoa/nativewidgets/main.cpp @@ -37,7 +37,7 @@ class ColorWidget : public QWidget void changeColor() { - color.setHsv((qreal(qrand()) / RAND_MAX) * 50 + 200, s, s); + color.setHsv(QRandomGenerator::global()->bounded(50) + 200, s, s); } public: diff --git a/tests/manual/textrendering/textperformance/main.cpp b/tests/manual/textrendering/textperformance/main.cpp index 9bd6d036af..eb6d09b9c8 100644 --- a/tests/manual/textrendering/textperformance/main.cpp +++ b/tests/manual/textrendering/textperformance/main.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include #include @@ -93,17 +94,17 @@ public: static const QString text = QLatin1String("Qt rocks!!!"); static const int textsPerPaint = 30; for (int i = 0; i < textsPerPaint; i++) { - const int fontSize = 4 + (qrand() % 5); - const int fontWeight = (qrand() % 2) == 1 ? QFont::Normal : QFont::Bold; - const bool fontItalic = (qrand() % 2) == 1; + const int fontSize = 4 + QRandomGenerator::global()->bounded(5); + const int fontWeight = QRandomGenerator::global()->bounded(2) == 1 ? QFont::Normal : QFont::Bold; + const bool fontItalic = QRandomGenerator::global()->bounded(2) == 1; const QFont font("Default", fontSize, fontWeight, fontItalic); p.setFont(font); - p.setPen(QColor::fromHsv(qrand() % 359, 155 + qrand() % 100, - 155 + qrand() % 100, 100 + qrand() % 155)); + p.setPen(QColor::fromHsv(QRandomGenerator::global()->bounded(359), 155 + QRandomGenerator::global()->bounded(100), + 155 + QRandomGenerator::global()->bounded(100), 100 + QRandomGenerator::global()->bounded(155))); const QSize textSize(p.fontMetrics().boundingRect(text).size()); const QPoint position( - -textSize.width() / 2 + (qrand() % size.width()), - textSize.height() / 2 + (qrand() % size.height())); + -textSize.width() / 2 + QRandomGenerator::global()->bounded(size.width()), + textSize.height() / 2 + QRandomGenerator::global()->bounded(size.height())); p.drawText(position, text); } } @@ -126,8 +127,8 @@ public: QString text; for (int i = 0; i < piecesPerPaint; ++i) { - QString piece = QLatin1String(pieces[qrand() % piecesCount]); - if (i == 0 || qrand() % 2) { + QString piece = QLatin1String(pieces[QRandomGenerator::global()->bounded(piecesCount)]); + if (i == 0 || QRandomGenerator::global()->bounded(2)) { // Make this piece the beginning of a new sentence. piece[0] = piece[0].toUpper(); if (i > 0) @@ -160,7 +161,7 @@ public: for (int i = 0; i < systemsPerPaint; i++) { if (i > 0) text.append(QLatin1Char(' ')); - text.append(samples.at(qrand() % samples.count())); + text.append(samples.at(QRandomGenerator::global()->bounded(samples.count()))); } p.drawText(QRectF(QPointF(0, 0), QSizeF(size)), Qt::AlignTop | Qt::AlignAbsolute | Qt::TextWordWrap, text); diff --git a/tests/manual/windowchildgeometry/controllerwidget.cpp b/tests/manual/windowchildgeometry/controllerwidget.cpp index 1d18c5d51b..871313d983 100644 --- a/tests/manual/windowchildgeometry/controllerwidget.cpp +++ b/tests/manual/windowchildgeometry/controllerwidget.cpp @@ -29,16 +29,12 @@ #include "controllerwidget.h" #include -#if QT_VERSION >= 0x050000 -# include -# include -# include -# include -# include -#else -# include -#endif - +#include +#include +#include +#include +#include +#include #include CoordinateControl::CoordinateControl(const QString &sep) : m_x(new QSpinBox), m_y(new QSpinBox) @@ -280,7 +276,7 @@ public: explicit Window(QWindow *parent = 0) : QWindow(parent) , m_backingStore(new QBackingStore(this)) - , m_color(Qt::GlobalColor(qrand() % 18)) + , m_color(Qt::GlobalColor(QRandomGenerator::global()->bounded(18))) { setObjectName(QStringLiteral("window")); setTitle(tr("TestWindow")); -- cgit v1.2.3