Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
QRandomGenerator Class Reference

\inmodule QtCore \reentrant More...

#include <qrandom.h>

+ Inheritance diagram for QRandomGenerator:
+ Collaboration diagram for QRandomGenerator:

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 QRandomGeneratoroperator= (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 ()
 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 QRandomGeneratorsystem ()
 \threadsafe
 
static Q_DECL_CONST_FUNCTION QRandomGeneratorglobal ()
 \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 () noexcept
 

Related Symbols

(Note that these are not member symbols.)

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.
 

Detailed Description

\inmodule QtCore \reentrant

Since
5.10

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.

Definition at line 20 of file qrandom.h.

Member Typedef Documentation

◆ result_type

A typedef to the type that operator() returns.

That is, quint32.

See also
operator()

Definition at line 165 of file qrandom.h.

Member Enumeration Documentation

◆ System

enum QRandomGenerator::System
protected

Definition at line 178 of file qrandom.h.

Constructor & Destructor Documentation

◆ QRandomGenerator() [1/7]

QRandomGenerator::QRandomGenerator ( quint32 seedValue = 1)
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.

See also
seed(), securelySeeded()

Definition at line 26 of file qrandom.h.

◆ QRandomGenerator() [2/7]

template<qsizetype N>
template< qsizetype N > QRandomGenerator::QRandomGenerator ( const quint32(&) seedBuffer[N])
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.

See also
seed(), securelySeeded()

Definition at line 29 of file qrandom.h.

◆ QRandomGenerator() [3/7]

QRandomGenerator::QRandomGenerator ( const quint32 * seedBuffer,
qsizetype len )
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 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:

std::seed_seq sseq(seedBuffer, seedBuffer + len);
See also
seed(), securelySeeded()

Definition at line 32 of file qrandom.h.

◆ QRandomGenerator() [4/7]

QRandomGenerator::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.

Two objects constructed or reseeded with the same seed value will produce the same number sequence.

See also
seed(), securelySeeded()

Definition at line 1192 of file qrandom.cpp.

References Q_ASSERT, sseq(), and storage.

+ Here is the call graph for this function:

◆ QRandomGenerator() [5/7]

QRandomGenerator::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.

Two objects constructed or reseeded with the same seed value will produce the same number sequence.

This constructor is equivalent to:

See also
seed(), securelySeeded()

Definition at line 1201 of file qrandom.cpp.

References begin(), QRandomGenerator::SystemAndGlobalGenerators::globalNoInit(), Q_ASSERT, and system().

+ Here is the call graph for this function:

◆ QRandomGenerator() [6/7]

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(), lock, other(), Q_ASSERT, system(), and SystemRNG.

+ Here is the call graph for this function:

◆ QRandomGenerator() [7/7]

QRandomGenerator::QRandomGenerator ( System )
inlineprotected

Definition at line 1162 of file qrandom.cpp.

Member Function Documentation

◆ bounded() [1/13]

double QRandomGenerator::bounded ( double highest)
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).

See also
generateDouble(), bounded()

Definition at line 72 of file qrandom.h.

References generateDouble().

Referenced by bounded(), bounded(), bounded(), bounded(), bounded(), bounded(), bounded(), bounded(), bounded(), and bounded().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ bounded() [2/13]

int QRandomGenerator::bounded ( int highest)
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.

See also
generate(), generate64(), generateDouble()

Definition at line 91 of file qrandom.h.

References bounded(), and Q_ASSERT.

+ Here is the call graph for this function:

◆ bounded() [3/13]

int QRandomGenerator::bounded ( int lowest,
int highest )
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.

See also
generate(), generate64(), generateDouble()

Definition at line 97 of file qrandom.h.

References bounded().

+ Here is the call graph for this function:

◆ bounded() [4/13]

qint64 QRandomGenerator::bounded ( int lowest,
qint64 highest )
inline

Definition at line 122 of file qrandom.h.

References bounded().

+ Here is the call graph for this function:

◆ bounded() [5/13]

qint64 QRandomGenerator::bounded ( qint64 highest)
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 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.

Note
This function is implemented as a loop, which depends on the random value obtained. On the long run, on average it should loop just under 2 times, but if the random generator is defective, this function may take considerably longer to execute.
See also
generate(), generate64(), generateDouble()

Definition at line 110 of file qrandom.h.

References bounded(), and Q_ASSERT.

+ Here is the call graph for this function:

◆ bounded() [6/13]

qint64 QRandomGenerator::bounded ( qint64 lowest,
int highest )
inline

Definition at line 126 of file qrandom.h.

References bounded().

+ Here is the call graph for this function:

◆ bounded() [7/13]

qint64 QRandomGenerator::bounded ( qint64 lowest,
qint64 highest )
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 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.

Note
This function is implemented as a loop, which depends on the random value obtained. On the long run, on average it should loop just under 2 times, but if the random generator is defective, this function may take considerably longer to execute.
See also
generate(), generate64(), generateDouble()

Definition at line 116 of file qrandom.h.

References bounded().

+ Here is the call graph for this function:

◆ bounded() [8/13]

quint32 QRandomGenerator::bounded ( quint32 highest)
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).

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().

See also
generate(), generate64(), generateDouble()

Definition at line 77 of file qrandom.h.

References generate(), and max().

+ Here is the call graph for this function:

◆ bounded() [9/13]

quint32 QRandomGenerator::bounded ( quint32 lowest,
quint32 highest )
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).

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().

See also
generate(), generate64(), generateDouble()

Definition at line 85 of file qrandom.h.

References bounded(), and Q_ASSERT.

+ Here is the call graph for this function:

◆ bounded() [10/13]

quint64 QRandomGenerator::bounded ( quint64 highest)
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 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().

Note
This function is implemented as a loop, which depends on the random value obtained. On the long run, on average it should loop just under 2 times, but if the random generator is defective, this function may take considerably longer to execute.
See also
generate(), generate64(), generateDouble()

Definition at line 252 of file qrandom.h.

References generate64(), and qCountLeadingZeroBits().

+ Here is the call graph for this function:

◆ bounded() [11/13]

quint64 QRandomGenerator::bounded ( quint64 lowest,
quint64 highest )
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 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().

Note
This function is implemented as a loop, which depends on the random value obtained. On the long run, on average it should loop just under 2 times, but if the random generator is defective, this function may take considerably longer to execute.
See also
generate(), generate64(), generateDouble()

Definition at line 104 of file qrandom.h.

References bounded(), and Q_ASSERT.

+ Here is the call graph for this function:

◆ bounded() [12/13]

quint64 QRandomGenerator::bounded ( quint64 lowest,
unsigned highest )
inline

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 135 of file qrandom.h.

References bounded().

+ Here is the call graph for this function:

◆ bounded() [13/13]

quint64 QRandomGenerator::bounded ( unsigned lowest,
quint64 highest )
inline

Definition at line 131 of file qrandom.h.

References bounded().

+ Here is the call graph for this function:

◆ discard()

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:

while (z--)

Definition at line 1211 of file qrandom.cpp.

References lock, Q_UNLIKELY, and SystemRNG.

Referenced by QRandomGenerator64::discard().

+ Here is the caller graph for this function:

◆ fillRange() [1/2]

template<typename UInt , IfValidUInt< UInt > = true>
template< typename UInt, QRandomGenerator::IfValidUInt< UInt >=true > void QRandomGenerator::fillRange ( UInt * buffer,
qsizetype count )
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:

QList<quint32> list;
list.resize(16);
See also
generate()

Definition at line 141 of file qrandom.h.

◆ fillRange() [2/2]

template<typename UInt , size_t N, IfValidUInt< UInt > = true>
template< typename UInt, size_t N, QRandomGenerator::IfValidUInt< UInt >=true > void QRandomGenerator::fillRange ( UInt(&) buffer[N])
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.

See also
generate()

Definition at line 147 of file qrandom.h.

◆ generate() [1/3]

quint32 QRandomGenerator::generate ( )
inline

Generates a 32-bit random quantity and returns it.

See also
{QRandomGenerator::operator()}{operator()()}, generate64()

Definition at line 48 of file qrandom.h.

Referenced by bounded(), generate(), QRandomGenerator::SystemGenerator::generate(), QQuickVectorImagePrivate::loadSvg(), operator()(), qCompileJSFile(), qCompileQmlFile(), QQuick3DParticleModelBlendParticle::randomIndex(), and QHttp2Connection::sendPing().

+ Here is the caller graph for this function:

◆ generate() [2/3]

template<typename ForwardIterator >
template< typename ForwardIterator > void QRandomGenerator::generate ( ForwardIterator begin,
ForwardIterator end )
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.

See also
fillRange()

Definition at line 154 of file qrandom.h.

References begin(), and generate().

+ Here is the call graph for this function:

◆ generate() [3/3]

void QRandomGenerator::generate ( quint32 * begin,
quint32 * end )
inline

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.

Definition at line 159 of file qrandom.h.

References begin().

+ Here is the call graph for this function:

◆ generate64()

quint64 QRandomGenerator::generate64 ( )
inline

Generates a 64-bit random quantity and returns it.

See also
{QRandomGenerator::operator()}{operator()()}, generate()

Definition at line 53 of file qrandom.h.

Referenced by bounded(), QRandomGenerator64::generate(), generateDouble(), and QRandomGenerator64::operator()().

+ Here is the caller graph for this function:

◆ generateDouble()

qreal QRandomGenerator::generateDouble ( )
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:

return std::generate_canonical<qreal, std::numeric_limits<qreal>::digits>(rd);

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.

See also
generate(), generate64(), bounded()

Definition at line 58 of file qrandom.h.

References generate64(), and Q_UINT64_C.

Referenced by bounded(), and QPRand::init().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ global()

QRandomGenerator * QRandomGenerator::global ( )
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.

See also
securelySeeded(), system()

Definition at line 275 of file qrandom.h.

References QRandomGenerator64::global().

Referenced by LowEnergyNotificationHub::LowEnergyNotificationHub(), QHttpMultiPartPrivate::QHttpMultiPartPrivate(), _q_PKCS12_salt(), QGradientCache::addCacheElement(), QQuickWanderAffector::affectParticle(), QBluetoothSocketPrivateBluezDBus::connectToServiceHelper(), QQuickParticleEmitter::emitWindow(), QQuickTrailEmitter::emitWindow(), QQuickParticleExtruder::extrude(), QQuickEllipseExtruder::extrude(), QQuickLineExtruder::extrude(), QQuickMaskExtruder::extrude(), QQuickRectangleExtruder::extrude(), QTemporaryFileName::generateNext(), QGeoUriProvider::getCurrentHost(), QQuickStochasticEngine::goalSeek(), QQuickImageParticle::initialize(), QV4::MathObject::method_random(), QQuickStochasticEngine::nextState(), qt_qdnsmailexchangerecord_sort(), qt_qdnsservicerecord_sort(), AndroidPositioning::registerPositionInfoSource(), QQuickStochasticEngine::restart(), QQuickSpriteEngine::restart(), QQuickAngleDirection::sample(), QQuickPointDirection::sample(), QQuickTargetDirection::sample(), QQuickViewTestUtils::StressTestModel::updateModel(), QQuickStochasticState::variedDuration(), QQuickSprite::variedDuration(), and QQmlJS::Dom::FileWriter::write().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ max()

result_type QRandomGenerator::max ( )
inlinestaticconstexpr

Returns the maximum value that QRandomGenerator may ever generate.

That is, {std::numeric_limits<result_type>::max()}.

See also
min(), QRandomGenerator64::max()

Definition at line 171 of file qrandom.h.

Referenced by bounded().

+ Here is the caller graph for this function:

◆ min()

result_type QRandomGenerator::min ( )
inlinestaticconstexpr

Returns the minimum value that QRandomGenerator may ever generate.

That is, 0.

See also
max(), QRandomGenerator64::min()

Definition at line 170 of file qrandom.h.

◆ operator()()

result_type QRandomGenerator::operator() ( )
inline

Generates a 32-bit random quantity and returns it.

See also
generate(), generate64()

Definition at line 166 of file qrandom.h.

References generate().

+ Here is the call graph for this function:

◆ operator=()

QRandomGenerator & QRandomGenerator::operator= ( const QRandomGenerator & other)

Definition at line 1180 of file qrandom.cpp.

References QRandomGenerator::SystemAndGlobalGenerators::globalNoInit(), lock, other(), Q_UNLIKELY, qFatal, system(), and SystemRNG.

+ Here is the call graph for this function:

◆ securelySeeded()

QRandomGenerator QRandomGenerator::securelySeeded ( )
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.

See also
global(), system()

Definition at line 280 of file qrandom.h.

References QRandomGenerator64::securelySeeded().

+ Here is the call graph for this function:

◆ seed() [1/2]

void QRandomGenerator::seed ( quint32 s = 1)
inline

Reseeds this object using the value seed as the seed.

Definition at line 167 of file qrandom.h.

Referenced by QPRand::init().

+ Here is the caller graph for this function:

◆ seed() [2/2]

void QRandomGenerator::seed ( std::seed_seq & sseq)
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.

Definition at line 168 of file qrandom.h.

References sseq().

+ Here is the call graph for this function:

◆ system()

QRandomGenerator * QRandomGenerator::system ( )
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.

See also
securelySeeded(), global()

Definition at line 270 of file qrandom.h.

References QRandomGenerator64::system().

Referenced by QAuthenticatorPrivate::QAuthenticatorPrivate(), QRandomGenerator(), QRandomGenerator(), QUuid::createUuid(), operator=(), and QTlsPrivate::TlsKeyGeneric::toPem().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Friends And Related Symbol Documentation

◆ operator!=

bool QRandomGenerator::operator!= ( const QRandomGenerator & rng1,
const QRandomGenerator & rng2 )
friend

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.

Definition at line 43 of file qrandom.h.

◆ operator==() [1/2]

bool operator== ( const QRandomGenerator & rng1,
const QRandomGenerator & rng2 )
related

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.

◆ operator== [2/2]

bool operator== ( const QRandomGenerator & rng1,
const QRandomGenerator & rng2 )
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.

◆ QRandomGenerator64

friend class QRandomGenerator64
friend

Definition at line 188 of file qrandom.h.

◆ qt_initial_random_value

InitialRandomData qt_initial_random_value ( )
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.


The documentation for this class was generated from the following files: