QtBase  v6.3.1
Classes | Public Types | Public Member Functions | Static Public Member Functions | Protected Types | Protected Member Functions | Friends | Related Functions | List of all members
QRandomGenerator Class Reference

The QRandomGenerator class allows one to obtain random values from a high-quality Random Number Generator. More...

#include <qrandom.h>

Inheritance diagram for QRandomGenerator:
Inheritance graph
[legend]

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

Detailed Description

The QRandomGenerator class allows one to obtain random values from a high-quality Random Number Generator.

\inmodule QtCore \reentrant

Since
5.10

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

Member Enumeration Documentation

◆ System

enum QRandomGenerator::System
protected

Definition at line 214 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 62 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 65 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:

See also
seed(), securelySeeded()

Definition at line 68 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 1228 of file qrandom.cpp.

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 1237 of file qrandom.cpp.

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 1204 of file qrandom.cpp.

Here is the call graph for this function:

◆ QRandomGenerator() [7/7]

QRandomGenerator::QRandomGenerator ( System  )
inlineprotected

Definition at line 1198 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 108 of file qrandom.h.

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

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

Here is the call graph for this function:

◆ bounded() [4/13]

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

Definition at line 158 of file qrandom.h.

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

Here is the call graph for this function:

◆ bounded() [6/13]

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

Definition at line 162 of file qrandom.h.

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

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

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

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

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

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

Here is the call graph for this function:

◆ bounded() [13/13]

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

Definition at line 167 of file qrandom.h.

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:

Definition at line 1247 of file qrandom.cpp.

Here is the caller graph for this function:

◆ fillRange() [1/2]

template<typename UInt , IfValidUInt< UInt > = true>
template< typename UInt > 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:

See also
generate()

Definition at line 177 of file qrandom.h.

Here is the caller graph for this function:

◆ fillRange() [2/2]

template<typename UInt , size_t N, IfValidUInt< UInt > = true>
template< typename UInt, size_t N > 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 183 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 84 of file qrandom.h.

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

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.

Definition at line 195 of file qrandom.h.

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

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:

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

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

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

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

Here is the call graph for this function:

◆ operator=()

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

Definition at line 1216 of file qrandom.cpp.

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

Here is the call graph for this function:

◆ seed() [1/2]

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

Reseeds this object using the value seed as the seed.

Definition at line 203 of file qrandom.h.

◆ seed() [2/2]

void QRandomGenerator::seed ( std::seed_seq &  seed)
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 204 of file qrandom.h.

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

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

Friends And Related Function Documentation

◆ operator!=

Returns true if the two 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 79 of file qrandom.h.

◆ operator==() [1/2]

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

◆ operator== [2/2]

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

◆ QRandomGenerator64

friend class QRandomGenerator64
friend

Definition at line 224 of file qrandom.h.

◆ qt_initial_random_value

InitialRandomData qt_initial_random_value ( )
friend

Definition at line 1324 of file qrandom.cpp.


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