![]() |
Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
|
\inmodule QtCore \reentrant More...
#include <qrandom.h>
Classes | |
struct | SystemAndGlobalGenerators |
struct | SystemGenerator |
Public Types | |
typedef quint32 | result_type |
A typedef to the type that operator() returns. | |
Public Member Functions | |
QRandomGenerator (quint32 seedValue=1) | |
Initializes this QRandomGenerator object with the value seedValue as the seed. | |
template<qsizetype N> | |
QRandomGenerator (const quint32(&seedBuffer)[N]) | |
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. | |
QRandomGenerator (const quint32 *seedBuffer, qsizetype len) | |
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. | |
Q_CORE_EXPORT | QRandomGenerator (std::seed_seq &sseq) 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. | |
Q_CORE_EXPORT | QRandomGenerator (const quint32 *begin, const quint32 *end) |
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. | |
Q_CORE_EXPORT | QRandomGenerator (const QRandomGenerator &other) |
Creates a copy of the generator state in the other object. | |
Q_CORE_EXPORT QRandomGenerator & | operator= (const QRandomGenerator &other) |
~QRandomGenerator ()=default | |
quint32 | generate () |
Generates a 32-bit random quantity and returns it. | |
quint64 | generate64 () |
Generates a 64-bit random quantity and returns it. | |
double | generateDouble () |
Generates one random qreal in the canonical range [0, 1) (that is, inclusive of zero and exclusive of 1). | |
double | bounded (double highest) |
Generates one random double in the range between 0 (inclusive) and highest (exclusive). | |
quint32 | bounded (quint32 highest) |
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). | |
quint32 | bounded (quint32 lowest, quint32 highest) |
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). | |
int | bounded (int highest) |
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). | |
int | bounded (int lowest, int highest) |
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. | |
quint64 | bounded (quint64 highest) |
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). | |
quint64 | bounded (quint64 lowest, quint64 highest) |
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). | |
qint64 | bounded (qint64 highest) |
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). | |
qint64 | bounded (qint64 lowest, qint64 highest) |
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. | |
qint64 | bounded (int lowest, qint64 highest) |
qint64 | bounded (qint64 lowest, int highest) |
quint64 | bounded (unsigned lowest, quint64 highest) |
quint64 | bounded (quint64 lowest, unsigned highest) |
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. | |
template<typename UInt, IfValidUInt< UInt > = true> | |
void | fillRange (UInt *buffer, qsizetype count) |
Generates count 32- or 64-bit quantities (depending on the type UInt ) and stores them in the buffer pointed by buffer. | |
template<typename UInt, size_t N, IfValidUInt< UInt > = true> | |
void | fillRange (UInt(&buffer)[N]) |
Generates N 32- or 64-bit quantities (depending on the type UInt ) and stores them in the buffer array. | |
template<typename ForwardIterator> | |
void | generate (ForwardIterator begin, ForwardIterator end) |
Generates 32-bit quantities and stores them in the range between begin and end. | |
void | generate (quint32 *begin, quint32 *end) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. | |
result_type | operator() () |
Generates a 32-bit random quantity and returns it. | |
void | seed (quint32 s=1) |
Reseeds this object using the value seed as the seed. | |
void | seed (std::seed_seq &sseq) noexcept |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Reseeds this object using the seed sequence seed as the seed. | |
Q_CORE_EXPORT void | discard (unsigned long long z) |
Discards the next z entries from the sequence. | |
Static Public Member Functions | |
static constexpr result_type | min () |
Returns the minimum value that QRandomGenerator may ever generate. | |
static constexpr result_type | max () |
Returns the maximum value that QRandomGenerator may ever generate. | |
static Q_DECL_CONST_FUNCTION QRandomGenerator * | system () |
\threadsafe | |
static Q_DECL_CONST_FUNCTION QRandomGenerator * | global () |
\threadsafe | |
static QRandomGenerator | securelySeeded () |
Returns a new QRandomGenerator object that was securely seeded with QRandomGenerator::system(). | |
Protected Types | |
enum | System |
Protected Member Functions | |
QRandomGenerator (System) | |
Friends | |
class | QRandomGenerator64 |
Q_CORE_EXPORT bool | operator== (const QRandomGenerator &rng1, const QRandomGenerator &rng2) |
Returns true if the two engines rng1 and rng2 are at the same state or if they are both reading from the operating system facilities, false otherwise. | |
bool | operator!= (const QRandomGenerator &rng1, const QRandomGenerator &rng2) |
Returns true if the two engines rng1 and rng2 are at different states or if one of them is reading from the operating system facilities and the other is not, false otherwise. | |
InitialRandomData | qt_initial_random_value () |
\inmodule QtCore \reentrant
The QRandomGenerator class allows one to obtain random values from a high-quality Random Number Generator.
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.
typedef quint32 QRandomGenerator::result_type |
|
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.
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 1192 of file qrandom.cpp.
References QRandomGenerator::SystemAndGlobalGenerators::globalNoInit(), and MersenneTwister.
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 1201 of file qrandom.cpp.
References QRandomGenerator::SystemAndGlobalGenerators::globalNoInit(), and MersenneTwister.
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 1168 of file qrandom.cpp.
References QRandomGenerator::SystemAndGlobalGenerators::globalNoInit(), and SystemRNG.
|
default |
|
inlineprotected |
Definition at line 1162 of file qrandom.cpp.
References SystemRNG.
|
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).
|
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.
|
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.
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.
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.
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().
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().
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().
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().
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.
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 1211 of file qrandom.cpp.
References SystemRNG.
|
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:
|
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.
|
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.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Same as the other overload, but more efficiently fills begin to end.
|
inline |
Generates a 64-bit random quantity and returns it.
|
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.
|
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.
|
inlinestaticconstexpr |
Returns the maximum value that QRandomGenerator may ever generate.
That is, {std::numeric_limits<result_type>::max()}
.
|
inlinestaticconstexpr |
Returns the minimum value that QRandomGenerator may ever generate.
That is, 0.
|
inline |
Generates a 32-bit random quantity and returns it.
QRandomGenerator & QRandomGenerator::operator= | ( | const QRandomGenerator & | other | ) |
Definition at line 1180 of file qrandom.cpp.
References SystemRNG.
|
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.
|
inlinenoexcept |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Reseeds this object using the seed sequence seed as the seed.
|
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.
|
friend |
|
friend |
Returns true if 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 1220 of file qrandom.cpp.
References SystemRNG.
|
friend |
|
friend |
Returns an initial random value (useful for QHash's global seed). This function attempts to use OS-provided random values to avoid initializing QRandomGenerator::system() and qsimd.cpp.
Note: on some systems, this functionn may rerturn the same value every time it is called.
Definition at line 1288 of file qrandom.cpp.