QRandomGenerator Class
The QRandomGenerator class allows one to obtain random values from a high-quality Random Number Generator. More…
| Header | #include <QRandomGenerator> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Core) |
| Module | Nt Core C++ Classes |
| Inherited by | QRandomGenerator64 |
List of all members, including inherited members
Public Types
| typedef | result_type |
Public Functions
| QRandomGenerator(quint32 seedValue = 1) | |
| QRandomGenerator(std::seed_seq &sseq) | |
| QRandomGenerator(const quint32 *seedBuffer, qsizetype len) | |
| QRandomGenerator(const quint32 (&seedBuffer)[N]) | |
| quint32 | generate() |
| quint64 | generate64() |
| double | generateDouble() |
| double | bounded(double highest) |
| double | bounded(double lowest, 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) |
| void | generate(It first, It last) |
| void | fillRange(quint32 *buffer, qsizetype count) |
| void | fillRange(quint64 *buffer, qsizetype count) |
| void | fillRange(UInt (&buffer)[N]) |
| result_type | operator()() |
| void | seed(quint32 s = 1) |
| void | discard(unsigned long long z) |
Static Public Members
| result_type | min() |
| result_type | max() |
| QRandomGenerator * | system() |
| QRandomGenerator * | global() |
| QRandomGenerator | securelySeeded() |
Detailed Description
A QRandomGenerator produces 32-bit and 64-bit random numbers. Generators that are constructed or seeded explicitly are deterministic: the same seed always gives the same sequence, which is useful for tests. The generators returned by system() and global() are not.
int roll = QRandomGenerator::global()->bounded(1, 7);
The class meets the requirements of a C++ UniformRandomBitGenerator, so it can be passed to the standard <random> distributions and to std::shuffle.
A seeded QRandomGenerator is not thread-safe. The objects returned by system() and global() are.
See also QUuid.
Differences from Qt
A seeded generator uses std::mt19937. system() reads from getrandom(2), and global() is guarded by a mutex rather than being thread-local.
On Zephyr system() reads getentropy(), which needs an entropy driver on the board. See Nt on Zephyr.
Member Type Documentation
typedef QRandomGenerator::result_type
The type of the values that the generator returns, quint32.
Member Function Documentation
QRandomGenerator::QRandomGenerator(quint32 seedValue = 1)
Constructs a generator seeded with seedValue. The same seed always produces the same sequence.
See also seed() and securelySeeded().
QRandomGenerator::QRandomGenerator(std::seed_seq &sseq)
This is an overloaded function.
Constructs a deterministic generator seeded from sseq.
This function was introduced in 5.10.
QRandomGenerator::QRandomGenerator(const quint32 *seedBuffer, qsizetype len)
Constructs a generator seeded from the len values at seedBuffer. The seed values are mixed with std::seed_seq, so the sequence is fully determined by them.
See also seed() and securelySeeded().
template <std::size_t N>QRandomGenerator::QRandomGenerator(const quint32 (&seedBuffer)[N])
This is an overloaded function.
Constructs a generator seeded from all N values of seedBuffer.
double QRandomGenerator::bounded(double highest)
This is an overloaded function.
Returns a random double that is greater than or equal to 0 and less than highest.
double QRandomGenerator::bounded(double lowest, double highest)
This is an overloaded function.
Returns a random double that is greater than or equal to lowest and less than highest.
quint32 QRandomGenerator::bounded(quint32 highest)
Returns a random value that is greater than or equal to 0 and less than highest. Returns 0 if highest is 0 or 1. The values are uniformly distributed.
See also generate() and generateDouble().
quint32 QRandomGenerator::bounded(quint32 lowest, quint32 highest)
This is an overloaded function.
Returns a random value that is greater than or equal to lowest and less than highest.
int QRandomGenerator::bounded(int highest)
This is an overloaded function.
Returns a random int that is greater than or equal to 0 and less than highest.
int QRandomGenerator::bounded(int lowest, int highest)
This is an overloaded function.
Returns a random int that is greater than or equal to lowest and less than highest.
quint64 QRandomGenerator::bounded(quint64 highest)
This is an overloaded function.
Returns a 64-bit random value that is greater than or equal to 0 and less than highest.
quint64 QRandomGenerator::bounded(quint64 lowest, quint64 highest)
This is an overloaded function.
Returns a random quint64 that is greater than or equal to lowest and less than highest.
This function was introduced in 6.4.
qint64 QRandomGenerator::bounded(qint64 highest)
This is an overloaded function.
Returns a random qint64 that is greater than or equal to 0 and less than highest.
qint64 QRandomGenerator::bounded(qint64 lowest, qint64 highest)
This is an overloaded function.
Returns a random qint64 that is greater than or equal to lowest and less than highest.
void QRandomGenerator::discard(unsigned long long z)
Discards the next z values of the sequence.
void QRandomGenerator::fillRange(quint32 *buffer, qsizetype count)
Fills buffer with count random 32-bit values.
See also generate().
void QRandomGenerator::fillRange(quint64 *buffer, qsizetype count)
This is an overloaded function.
Fills buffer with count random 64-bit values.
template <typename UInt, std::size_t N, typename = std::enable_if_t<std::is_unsigned_v<UInt> && (sizeof(UInt) <= 8)>>void QRandomGenerator::fillRange(UInt (&buffer)[N])
This is an overloaded function.
Fills the array buffer with random values of its element type, which must be an unsigned integer type of at most 64 bits.
quint32 QRandomGenerator::generate()
Returns a random 32-bit value from this generator.
See also generate64() and bounded().
template <typename It>void QRandomGenerator::generate(It first, It last)
This is an overloaded function.
Stores a random 32-bit value in each element of the range from first to last.
quint64 QRandomGenerator::generate64()
Returns a random 64-bit value, built from two calls to generate().
See also generate() and bounded().
double QRandomGenerator::generateDouble()
Returns a random double that is greater than or equal to 0 and less than 1, with 53 bits of randomness.
See also bounded().
QRandomGenerator * QRandomGenerator::global()
Returns the process-wide pseudo-random generator, which is seeded once from system(). It is thread-safe but not suitable for cryptographic use.
See also system() and securelySeeded().
result_type QRandomGenerator::max()
Returns the largest value that generate() can return, 0xffffffff.
See also min().
result_type QRandomGenerator::min()
Returns the smallest value that generate() can return, 0.
See also max().
result_type QRandomGenerator::operator()()
Returns a random 32-bit value, the same as generate(). This lets the generator be used with the C++ standard library's distributions.
QRandomGenerator QRandomGenerator::securelySeeded()
Returns a new generator that is seeded with random data from system().
See also system() and global().
void QRandomGenerator::seed(quint32 s = 1)
Reseeds the generator with s, which makes it a deterministic generator.
See also discard().
QRandomGenerator * QRandomGenerator::system()
Returns the process-wide generator that reads from the operating system's random number source. It is cryptographically secure and thread-safe, but slower than global().
See also global() and securelySeeded().