![]() |
Qt 6.x
The Qt SDK
|
\inmodule QtCore More...
#include <qrandom.h>
Public Types | |
typedef quint64 | result_type |
A typedef to the type that operator() returns. | |
![]() | |
typedef quint32 | result_type |
A typedef to the type that operator() returns. | |
Public Member Functions | |
quint64 | generate () |
Generates one 64-bit random value and returns it. | |
result_type | operator() () |
Generates a 64-bit random quantity and returns it. | |
QRandomGenerator64 (quint32 seedValue=1) | |
template<qsizetype N> | |
QRandomGenerator64 (const quint32(&seedBuffer)[N]) | |
QRandomGenerator64 (const quint32 *seedBuffer, qsizetype len) | |
QRandomGenerator64 (std::seed_seq &sseq) noexcept | |
QRandomGenerator64 (const quint32 *begin, const quint32 *end) | |
QRandomGenerator64 (const QRandomGenerator &other) | |
void | discard (unsigned long long z) |
quint32 | generate () |
Generates a 32-bit random quantity and returns it. | |
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. | |
![]() | |
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) |
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 () |
static constexpr result_type | max () |
static Q_DECL_CONST_FUNCTION Q_CORE_EXPORT QRandomGenerator64 * | system () |
static Q_DECL_CONST_FUNCTION Q_CORE_EXPORT QRandomGenerator64 * | global () |
static Q_CORE_EXPORT QRandomGenerator64 | securelySeeded () |
![]() | |
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(). | |
Additional Inherited Members | |
![]() | |
enum | System |
![]() | |
QRandomGenerator (System) | |
![]() | |
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. | |
\inmodule QtCore
The QRandomGenerator64 class allows one to obtain 64-bit random values from a high-quality, seed-less Random Number Generator.
QRandomGenerator64 is a simple adaptor class around QRandomGenerator, making the QRandomGenerator::generate64() function the default for operator()(), instead of the function that returns 32-bit quantities. This class is intended to be used in conjunction with Standard Library algorithms that need 64-bit quantities instead of 32-bit ones.
In all other aspects, the class is the same. Please refer to QRandomGenerator's documentation for more information.
|
inline |
|
inlinenoexcept |
|
inline |
|
inline |
Definition at line 237 of file qrandom.h.
References QRandomGenerator::discard(), and Q_ASSERT_X.
|
inline |
Generates a 32-bit random quantity and returns it.
|
inline |
Generates one 64-bit random value and returns it.
Note about casting to a signed integer: all bits returned by this function are random, so there's a 50% chance that the most significant bit will be set. If you wish to cast the returned value to qint64 and keep it positive, you should mask the sign bit off:
Definition at line 214 of file qrandom.h.
References QRandomGenerator::generate64().
|
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.
|
static |
Definition at line 1133 of file qrandom.cpp.
References QRandomGenerator::SystemAndGlobalGenerators::globalNoInit(), Q_LIKELY, QRandomGenerator::SystemAndGlobalGenerators::securelySeed(), and SystemRNG.
Referenced by QRandomGenerator::global().
|
inlinestaticconstexpr |
|
inlinestaticconstexpr |
|
inline |
Generates a 64-bit random quantity and returns it.
Definition at line 217 of file qrandom.h.
References QRandomGenerator::generate64().
|
static |
Definition at line 1151 of file qrandom.cpp.
References QRandomGenerator::SystemAndGlobalGenerators::securelySeed().
Referenced by QRandomGenerator::securelySeeded().
|
static |
Definition at line 1126 of file qrandom.cpp.
References Q_ASSERT, QRandomGenerator::SystemAndGlobalGenerators::system(), and SystemRNG.
Referenced by QRandomGenerator::system().