summaryrefslogtreecommitdiffstats
path: root/src/corelib/global/qrandom.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/global/qrandom.cpp')
-rw-r--r--src/corelib/global/qrandom.cpp83
1 files changed, 18 insertions, 65 deletions
diff --git a/src/corelib/global/qrandom.cpp b/src/corelib/global/qrandom.cpp
index ebf9864b15..23e5e499b2 100644
--- a/src/corelib/global/qrandom.cpp
+++ b/src/corelib/global/qrandom.cpp
@@ -510,9 +510,7 @@ inline QRandomGenerator::SystemGenerator &QRandomGenerator::SystemGenerator::sel
those. The most common way of generating new values is to call the generate(),
generate64() or fillRange() functions. One would use it as:
- \code
- quint32 value = QRandomGenerator::global()->generate();
- \endcode
+ \snippet code/src_corelib_global_qrandom.cpp 0
Additionally, it provides a floating-point function generateDouble() that
returns a number in the range [0, 1) (that is, inclusive of zero and
@@ -525,11 +523,7 @@ inline QRandomGenerator::SystemGenerator &QRandomGenerator::SystemGenerator::sel
the numbers generated by the object will always be the same, as in the
following example:
- \code
- QRandomGenerator prng1(1234), prng2(1234);
- Q_ASSERT(prng1.generate32() == prng2.generate32());
- Q_ASSERT(prng1.generate64() == prng2.generate64());
- \endcode
+ \snippet code/src_corelib_global_qrandom.cpp 1
The seed data takes the form of one or more 32-bit words. The ideal seed
size is approximately equal to the size of the QRandomGenerator class
@@ -552,12 +546,7 @@ inline QRandomGenerator::SystemGenerator &QRandomGenerator::SystemGenerator::sel
For ease of use, QRandomGenerator provides a global object that can
be easily used, as in the following example:
- \code
- int x = QRandomGenerator::global()->generate32();
- int y = QRandomGenerator::global()->generate32();
- int w = QRandomGenerator::global()->bounded(16384);
- int h = QRandomGenerator::global()->bounded(16384);
- \endcode
+ \snippet code/src_corelib_global_qrandom.cpp 2
\section1 System-wide random number generator
@@ -645,10 +634,7 @@ inline QRandomGenerator::SystemGenerator &QRandomGenerator::SystemGenerator::sel
following code may be used to generate a floating-point number in the range
[1, 2.5):
- \code
- std::uniform_real_distribution dist(1, 2.5);
- return dist(*QRandomGenerator::global());
- \endcode
+ \snippet code/src_corelib_global_qrandom.cpp 3
\sa QRandomGenerator64, qrand()
*/
@@ -688,10 +674,7 @@ inline QRandomGenerator::SystemGenerator &QRandomGenerator::SystemGenerator::sel
with the same seed value will produce the same number sequence.
This constructor is equivalent to:
- \code
- std::seed_seq sseq(seedBuffer, seedBuffer + len);
- QRandomGenerator generator(sseq);
- \endcode
+ \snippet code/src_corelib_global_qrandom.cpp 4
\sa seed(), securelySeeded()
*/
@@ -705,10 +688,7 @@ inline QRandomGenerator::SystemGenerator &QRandomGenerator::SystemGenerator::sel
with the same seed value will produce the same number sequence.
This constructor is equivalent to:
- \code
- std::seed_seq sseq(begin, end);
- QRandomGenerator generator(sseq);
- \endcode
+ \snippet code/src_corelib_global_qrandom.cpp 5
\sa seed(), securelySeeded()
*/
@@ -828,10 +808,7 @@ inline QRandomGenerator::SystemGenerator &QRandomGenerator::SystemGenerator::sel
Discards the next \a z entries from the sequence. This method is equivalent
to calling generate() \a z times and discarding the result, as in:
- \code
- while (z--)
- generator.generate();
- \endcode
+ \snippet code/src_corelib_global_qrandom.cpp 6
*/
/*!
@@ -840,9 +817,7 @@ inline QRandomGenerator::SystemGenerator &QRandomGenerator::SystemGenerator::sel
Generates 32-bit quantities and stores them in the range between \a begin
and \a end. This function is equivalent to (and is implemented as):
- \code
- std::generate(begin, end, [this]() { return generate(); });
- \endcode
+ \snippet code/src_corelib_global_qrandom.cpp 7
This function complies with the requirements for the function
\l{http://en.cppreference.com/w/cpp/numeric/random/seed_seq/generate}{\c std::seed_seq::generate},
@@ -853,9 +828,7 @@ inline QRandomGenerator::SystemGenerator &QRandomGenerator::SystemGenerator::sel
32 bits of data. Any other bits will be zero. To fill the range with 64 bit
quantities, one can write:
- \code
- std::generate(begin, end, []() { return QRandomGenerator::global()->generate64(); });
- \endcode
+ \snippet code/src_corelib_global_qrandom.cpp 8
If the range refers to contiguous memory (such as an array or the data from
a QVector), the fillRange() function may be used too.
@@ -882,11 +855,7 @@ inline QRandomGenerator::SystemGenerator &QRandomGenerator::SystemGenerator::sel
For example, to fill a vector of 16 entries with random values, one may
write:
- \code
- QVector<quint32> vector;
- vector.resize(16);
- QRandomGenerator::fillRange(vector.data(), vector.size());
- \endcode
+ \snippet code/src_corelib_global_qrandom.cpp 9
\sa generate()
*/
@@ -901,10 +870,7 @@ inline QRandomGenerator::SystemGenerator &QRandomGenerator::SystemGenerator::sel
For example, to fill generate two 32-bit quantities, one may write:
- \code
- quint32 array[2];
- QRandomGenerator::fillRange(array);
- \endcode
+ \snippet code/src_corelib_global_qrandom.cpp 10
It would have also been possible to make one call to generate64() and then split
the two halves of the 64-bit value.
@@ -919,10 +885,7 @@ inline QRandomGenerator::SystemGenerator &QRandomGenerator::SystemGenerator::sel
inclusive of zero and exclusive of 1).
This function is equivalent to:
- \code
- QRandomGenerator64 rd;
- return std::generate_canonical<qreal, std::numeric_limits<qreal>::digits>(rd);
- \endcode
+ \snippet code/src_corelib_global_qrandom.cpp 11
The same may also be obtained by using
\l{http://en.cppreference.com/w/cpp/numeric/random/uniform_real_distribution}{\c std::uniform_real_distribution}
@@ -937,9 +900,7 @@ inline QRandomGenerator::SystemGenerator &QRandomGenerator::SystemGenerator::sel
Generates one random double in the range between 0 (inclusive) and \a
highest (exclusive). This function is equivalent to and is implemented as:
- \code
- return generateDouble() * highest;
- \endcode
+ \snippet code/src_corelib_global_qrandom.cpp 12
\sa generateDouble(), bounded()
*/
@@ -956,9 +917,7 @@ inline QRandomGenerator::SystemGenerator &QRandomGenerator::SystemGenerator::sel
For example, to obtain a value between 0 and 255 (inclusive), one would write:
- \code
- quint32 v = QRandomGenerator::bounded(256);
- \endcode
+ \snippet code/src_corelib_global_qrandom.cpp 13
Naturally, the same could also be obtained by masking the result of generate()
to only the lower 8 bits. Either solution is as efficient.
@@ -995,9 +954,7 @@ inline QRandomGenerator::SystemGenerator &QRandomGenerator::SystemGenerator::sel
For example, to obtain a value between 1000 (incl.) and 2000 (excl.), one
would write:
- \code
- quint32 v = QRandomGenerator::bounded(1000, 2000);
- \endcode
+ \snippet code/src_corelib_global_qrandom.cpp 14
Note that this function cannot be used to obtain values in the full 32-bit
@@ -1053,9 +1010,7 @@ inline QRandomGenerator::SystemGenerator &QRandomGenerator::SystemGenerator::sel
For example, the following creates a random RGB color:
- \code
- return QColor::fromRgb(QRandomGenerator::global()->generate());
- \endcode
+ \snippet code/src_corelib_global_qrandom.cpp 15
Accesses to this object are thread-safe and it may therefore be used in any
thread without locks. The object may also be copied and the sequence
@@ -1123,9 +1078,7 @@ inline QRandomGenerator::SystemGenerator &QRandomGenerator::SystemGenerator::sel
set. If you wish to cast the returned value to qint64 and keep it positive,
you should mask the sign bit off:
- \code
- qint64 value = QRandomGenerator64::generate() & std::numeric_limits<qint64>::max();
- \endcode
+ \snippet code/src_corelib_global_qrandom.cpp 16
\sa QRandomGenerator, QRandomGenerator::generate64()
*/
@@ -1298,7 +1251,7 @@ struct QRandEngine
};
}
-#if defined(QT_NO_THREAD) || defined(Q_OS_WIN)
+#if defined(Q_OS_WIN)
// On Windows srand() and rand() already use Thread-Local-Storage
// to store the seed between calls
static inline QRandEngine *randTLS()