Doc: add a note about QRandomGenerator returning the sign bit set

Found this in a few uses of qrand() that assumed the result would be
non-negative.

Change-Id: Ia53158e207a94bf49489fffd14c7c029515cf42c
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
bb10
Thiago Macieira 2017-06-13 10:58:04 -07:00
parent 0669f71b0c
commit fffbdb4c37
1 changed files with 18 additions and 0 deletions

View File

@ -802,6 +802,15 @@ static Q_NEVER_INLINE void fill(void *buffer, void *bufferEnd)
/*!
Generates one 32-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 int and keep it positive,
you should mask the sign bit off:
\code
int value = QRandomGenerator::get32() & std::numeric_limits<int>::max();
\endcode
\sa get64(), getReal()
*/
quint32 QRandomGenerator::get32()
@ -814,6 +823,15 @@ quint32 QRandomGenerator::get32()
/*!
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:
\code
qint64 value = QRandomGenerator::get64() & std::numeric_limits<qint64>::max();
\endcode
\sa get32(), getReal(), QRandomGenerator64
*/
quint64 QRandomGenerator::get64()