diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/global/qrandom.h | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/src/corelib/global/qrandom.h b/src/corelib/global/qrandom.h index 7f96cd6749..2259f2657a 100644 --- a/src/corelib/global/qrandom.h +++ b/src/corelib/global/qrandom.h @@ -62,8 +62,16 @@ public: static Q_CORE_EXPORT quint64 generate64(); static double generateDouble() { - // use get64() to get enough bits - return double(generate64()) / ((std::numeric_limits<quint64>::max)() + double(1.0)); + // IEEE 754 double precision has: + // 1 bit sign + // 10 bits exponent + // 53 bits mantissa + // In order for our result to be normalized in the range [0, 1), we + // need exactly 53 bits of random data. Use generate64() to get enough. + quint64 x = generate64(); + quint64 limit = Q_UINT64_C(1) << std::numeric_limits<double>::digits; + x >>= std::numeric_limits<quint64>::digits - std::numeric_limits<double>::digits; + return double(x) / double(limit); } static qreal bounded(qreal sup) |