QtBase
v6.3.1
|
The QRandomGenerator class allows one to obtain random values from a high-quality Random Number Generator. More...
#include <qrandom.h>
Classes | |
struct | SystemAndGlobalGenerators |
struct | SystemGenerator |
Public Types | |
typedef quint32 | result_type |
Public Member Functions | |
QRandomGenerator (quint32 seedValue=1) | |
template<qsizetype N> | |
QRandomGenerator (const quint32(&seedBuffer)[N]) | |
QRandomGenerator (const quint32 *seedBuffer, qsizetype len) | |
Q_CORE_EXPORT | QRandomGenerator (std::seed_seq &sseq) noexcept |
Q_CORE_EXPORT | QRandomGenerator (const quint32 *begin, const quint32 *end) |
Q_CORE_EXPORT | QRandomGenerator (const QRandomGenerator &other) |
Q_CORE_EXPORT QRandomGenerator & | operator= (const QRandomGenerator &other) |
quint32 | generate () |
quint64 | generate64 () |
double | generateDouble () |
double | bounded (double highest) |
quint32 | bounded (quint32 highest) |
quint32 | bounded (quint32 lowest, quint32 highest) |
int | bounded (int highest) |
int | bounded (int lowest, int highest) |
quint64 | bounded (quint64 highest) |
quint64 | bounded (quint64 lowest, quint64 highest) |
qint64 | bounded (qint64 highest) |
qint64 | bounded (qint64 lowest, qint64 highest) |
qint64 | bounded (int lowest, qint64 highest) |
qint64 | bounded (qint64 lowest, int highest) |
quint64 | bounded (unsigned lowest, quint64 highest) |
quint64 | bounded (quint64 lowest, unsigned highest) |
template<typename UInt , IfValidUInt< UInt > = true> | |
void | fillRange (UInt *buffer, qsizetype count) |
template<typename UInt , size_t N, IfValidUInt< UInt > = true> | |
void | fillRange (UInt(&buffer)[N]) |
template<typename ForwardIterator > | |
void | generate (ForwardIterator begin, ForwardIterator end) |
void | generate (quint32 *begin, quint32 *end) |
result_type | operator() () |
void | seed (quint32 s=1) |
void | seed (std::seed_seq &sseq) noexcept |
Q_CORE_EXPORT void | discard (unsigned long long z) |
Static Public Member Functions | |
static constexpr result_type | min () |
static constexpr result_type | max () |
static Q_DECL_CONST_FUNCTION QRandomGenerator * | system () |
static Q_DECL_CONST_FUNCTION QRandomGenerator * | global () |
static QRandomGenerator | securelySeeded () |
Protected Types | |
enum | System |
Protected Member Functions | |
QRandomGenerator (System) | |
Friends | |
class | QRandomGenerator64 |
Q_CORE_EXPORT bool | operator== (const QRandomGenerator &rng1, const QRandomGenerator &rng2) |
bool | operator!= (const QRandomGenerator &rng1, const QRandomGenerator &rng2) |
InitialRandomData | qt_initial_random_value () noexcept |
Related Functions | |
(Note that these are not member functions.) | |
bool | operator== (const QRandomGenerator &rng1, const QRandomGenerator &rng2) |
The QRandomGenerator class allows one to obtain random values from a high-quality Random Number Generator.
\inmodule QtCore \reentrant
QRandomGenerator may be used to generate random values from a high-quality random number generator. Like the C++ random engines, QRandomGenerator can be seeded with user-provided values through the constructor. When seeded, the sequence of numbers generated by this class is deterministic. That is to say, given the same seed data, QRandomGenerator will generate the same sequence of numbers. But given different seeds, the results should be considerably different.
QRandomGenerator::securelySeeded() can be used to create a QRandomGenerator that is securely seeded with QRandomGenerator::system(), meaning that the sequence of numbers it generates cannot be easily predicted. Additionally, QRandomGenerator::global() returns a global instance of QRandomGenerator that Qt will ensure to be securely seeded. This object is thread-safe, may be shared for most uses, and is always seeded from QRandomGenerator::system()
QRandomGenerator::system() may be used to access the system's cryptographically-safe random generator. On Unix systems, it's equivalent to reading from {/dev/urandom}
or the {getrandom()}
or {getentropy()}
system calls.
The class can generate 32-bit or 64-bit quantities, or fill an array of those. The most common way of generating new values is to call the generate(), generate64() or fillRange() functions. One would use it as:
Additionally, it provides a floating-point function generateDouble() that returns a number in the range [0, 1) (that is, inclusive of zero and exclusive of 1). There's also a set of convenience functions that facilitate obtaining a random number in a bounded, integral range.
|
protected |
|
inline |
Initializes this QRandomGenerator object with the value seedValue as the seed. Two objects constructed or reseeded with the same seed value will produce the same number sequence.
|
inline |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Initializes this QRandomGenerator object with the values found in the array seedBuffer as the seed. Two objects constructed or reseeded with the same seed value will produce the same number sequence.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Initializes this QRandomGenerator object with len values found in the array seedBuffer as the seed. Two objects constructed or reseeded with the same seed value will produce the same number sequence.
This constructor is equivalent to:
|
noexcept |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Initializes this QRandomGenerator object with the seed sequence sseq as the seed. Two objects constructed or reseeded with the same seed value will produce the same number sequence.
Definition at line 1228 of file qrandom.cpp.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Initializes this QRandomGenerator object with the values found in the range from begin to end as the seed. Two objects constructed or reseeded with the same seed value will produce the same number sequence.
This constructor is equivalent to:
Definition at line 1237 of file qrandom.cpp.
QRandomGenerator::QRandomGenerator | ( | const QRandomGenerator & | other | ) |
Creates a copy of the generator state in the other object. If other is QRandomGenerator::system() or a copy of that, this object will also read from the operating system random-generating facilities. In that case, the sequences generated by the two objects will be different.
In all other cases, the new QRandomGenerator object will start at the same position in the deterministic sequence as the other object was. Both objects will generate the same sequence from this point on.
For that reason, it is not advisable to create a copy of QRandomGenerator::global(). If one needs an exclusive deterministic generator, consider instead using securelySeeded() to obtain a new object that shares no relationship with the QRandomGenerator::global().
Definition at line 1204 of file qrandom.cpp.
|
inlineprotected |
Definition at line 1198 of file qrandom.cpp.
|
inline |
Generates one random double in the range between 0 (inclusive) and highest (exclusive). This function is equivalent to and is implemented as:
If the highest parameter is negative, the result will be negative too; if it is infinite or NaN, the result will be infinite or NaN too (that is, not random).
Definition at line 108 of file qrandom.h.
|
inline |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Generates one random 32-bit quantity in the range between 0 (inclusive) and highest (exclusive). highest must be positive.
Note that this function cannot be used to obtain values in the full 32-bit range of int. Instead, use generate() and cast to int.
Definition at line 127 of file qrandom.h.
|
inline |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Generates one random 32-bit quantity in the range between lowest (inclusive) and highest (exclusive), both of which may be negative, but highest must be greater than lowest.
Note that this function cannot be used to obtain values in the full 32-bit range of int. Instead, use generate() and cast to int.
Definition at line 133 of file qrandom.h.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Generates one random 64-bit quantity in the range between 0 (inclusive) and highest (exclusive). highest must be positive.
Note that this function cannot be used to obtain values in the full 64-bit range of {qint64}. Instead, use generate64() and cast to qint64 or instead use the unsigned version of this function.
Definition at line 146 of file qrandom.h.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Generates one random 64-bit quantity in the range between lowest (inclusive) and highest (exclusive), both of which may be negative, but highest must be greater than lowest.
Note that this function cannot be used to obtain values in the full 64-bit range of {qint64}. Instead, use generate64() and cast to qint64.
Definition at line 152 of file qrandom.h.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Generates one random 32-bit quantity in the range between 0 (inclusive) and highest (exclusive). The same result may also be obtained by using \l{http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution}{std::uniform_int_distribution}
with parameters 0 and {highest - 1}. That class can also be used to obtain quantities larger than 32 bits; for 64 bits, the 64-bit bounded() overload can be used too.
For example, to obtain a value between 0 and 255 (inclusive), one would write:
Naturally, the same could also be obtained by masking the result of generate() to only the lower 8 bits. Either solution is as efficient.
Note that this function cannot be used to obtain values in the full 32-bit range of quint32. Instead, use generate().
Definition at line 113 of file qrandom.h.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Generates one random 32-bit quantity in the range between lowest (inclusive) and highest (exclusive). The highest parameter must be greater than lowest.
The same result may also be obtained by using \l{http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution}{std::uniform_int_distribution}
with parameters lowest and {highest - 1}. That class can also be used to obtain quantities larger than 32 bits.
For example, to obtain a value between 1000 (incl.) and 2000 (excl.), one would write:
Note that this function cannot be used to obtain values in the full 32-bit range of quint32. Instead, use generate().
Definition at line 121 of file qrandom.h.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Generates one random 64-bit quantity in the range between 0 (inclusive) and highest (exclusive). The same result may also be obtained by using \l{http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution}{std::uniform_int_distribution<quint64>}
with parameters 0 and {highest - 1}.
Note that this function cannot be used to obtain values in the full 64-bit range of {quint64}. Instead, use generate64().
Definition at line 288 of file qrandom.h.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Generates one random 64-bit quantity in the range between lowest (inclusive) and highest (exclusive). The highest parameter must be greater than lowest.
The same result may also be obtained by using \l{http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution}{std::uniform_int_distribution<quint64>}
with parameters lowest and {highest - 1}.
Note that this function cannot be used to obtain values in the full 64-bit range of {quint64}. Instead, use generate64().
Definition at line 140 of file qrandom.h.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
This function exists to help with overload resolution when the types of the parameters don't exactly match. They will promote the smaller type to the type of the larger one and call the correct overload.
Definition at line 171 of file qrandom.h.
void QRandomGenerator::discard | ( | unsigned long long | z | ) |
Discards the next z entries from the sequence. This method is equivalent to calling generate() z times and discarding the result, as in:
Definition at line 1247 of file qrandom.cpp.
|
inline |
Generates count 32- or 64-bit quantities (depending on the type UInt
) and stores them in the buffer pointed by buffer. This is the most efficient way to obtain more than one quantity at a time, as it reduces the number of calls into the Random Number Generator source.
For example, to fill a list of 16 entries with random values, one may write:
Definition at line 177 of file qrandom.h.
|
inline |
Generates N
32- or 64-bit quantities (depending on the type UInt
) and stores them in the buffer array. This is the most efficient way to obtain more than one quantity at a time, as it reduces the number of calls into the Random Number Generator source.
For example, to fill generate two 32-bit quantities, one may write:
It would have also been possible to make one call to generate64() and then split the two halves of the 64-bit value.
|
inline |
Generates a 32-bit random quantity and returns it.
Definition at line 84 of file qrandom.h.
|
inline |
Generates 32-bit quantities and stores them in the range between begin and end. This function is equivalent to (and is implemented as):
This function complies with the requirements for the function \l{http://en.cppreference.com/w/cpp/numeric/random/seed_seq/generate}{std::seed_seq::generate}
, which requires unsigned 32-bit integer values.
Note that if the [begin, end) range refers to an area that can store more than 32 bits per element, the elements will still be initialized with only 32 bits of data. Any other bits will be zero. To fill the range with 64 bit quantities, one can write:
If the range refers to contiguous memory (such as an array or the data from a QList), the fillRange() function may be used too.
Definition at line 190 of file qrandom.h.
|
inline |
Generates a 64-bit random quantity and returns it.
Definition at line 89 of file qrandom.h.
|
inline |
Generates one random qreal in the canonical range [0, 1) (that is, inclusive of zero and exclusive of 1).
This function is equivalent to:
The same may also be obtained by using \l{http://en.cppreference.com/w/cpp/numeric/random/uniform_real_distribution}{std::uniform_real_distribution}
with parameters 0 and 1.
Definition at line 94 of file qrandom.h.
|
inlinestatic |
\threadsafe
Returns a pointer to a shared QRandomGenerator that was seeded using securelySeeded(). This function should be used to create random data without the expensive creation of a securely-seeded QRandomGenerator for a specific use or storing the rather large QRandomGenerator object.
For example, the following creates a random RGB color:
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 produced by the copy will be the same as the shared object will produce. Note, however, that if there are other threads accessing the global object, those threads may obtain samples at unpredictable intervals.
Definition at line 311 of file qrandom.h.
|
inlinestaticconstexpr |
Returns the maximum value that QRandomGenerator may ever generate. That is, {std::numeric_limits<result_type>::max()}
.
Definition at line 207 of file qrandom.h.
|
inlinestaticconstexpr |
Returns the minimum value that QRandomGenerator may ever generate. That is, 0.
|
inline |
Generates a 32-bit random quantity and returns it.
Definition at line 202 of file qrandom.h.
QRandomGenerator & QRandomGenerator::operator= | ( | const QRandomGenerator & | other | ) |
|
inlinestatic |
Returns a new QRandomGenerator object that was securely seeded with QRandomGenerator::system(). This function will obtain the ideal seed size for the algorithm that QRandomGenerator uses and is therefore the recommended way for creating a new QRandomGenerator object that will be kept for some time.
Given the amount of data required to securely seed the deterministic engine, this function is somewhat expensive and should not be used for short-term uses of QRandomGenerator (using it to generate fewer than 2600 bytes of random data is effectively a waste of resources). If the use doesn't require that much data, consider using QRandomGenerator::global() and not storing a QRandomGenerator object instead.
Definition at line 316 of file qrandom.h.
|
inlinenoexcept |
|
inlinestatic |
\threadsafe
Returns a pointer to a shared QRandomGenerator that always uses the facilities provided by the operating system to generate random numbers. The system facilities are considered to be cryptographically safe on at least the following operating systems: Apple OSes (Darwin), BSDs, Linux, Windows. That may also be the case on other operating systems.
They are also possibly backed by a true hardware random number generator. For that reason, the QRandomGenerator returned by this function should not be used for bulk data generation. Instead, use it to seed QRandomGenerator or a random engine from the <random> header.
The object returned by this function is thread-safe and may be used in any thread without locks. It may also be copied and the resulting QRandomGenerator will also access the operating system facilities, but they will not generate the same sequence.
Definition at line 306 of file qrandom.h.
|
friend |
|
related |
Returns true if the two the two engines rng1 and rng2 are at the same state or if they are both reading from the operating system facilities, false otherwise.
Definition at line 1256 of file qrandom.cpp.
|
friend |
Returns true if the two the two engines rng1 and rng2 are at the same state or if they are both reading from the operating system facilities, false otherwise.
Definition at line 1256 of file qrandom.cpp.
|
friend |
|
friend |
Definition at line 1324 of file qrandom.cpp.