diff options
-rw-r--r-- | src/corelib/global/qrandom.cpp | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/src/corelib/global/qrandom.cpp b/src/corelib/global/qrandom.cpp index 6a92260c46..66f21bc7f0 100644 --- a/src/corelib/global/qrandom.cpp +++ b/src/corelib/global/qrandom.cpp @@ -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() |