qt5base-lts/tests/auto/corelib/global
Thiago Macieira 21d3916817 QRandomGenerator: add 64-bit bounded() versions
Unlike the 32-bit version, we can't go to a bigger integer type to do
the multiplication with. So instead accept looping. Both libstdc++ and
libc++ implement std::uniform_int_distribution this way anyway, but in a
far more complex way.

There is no looping if the "highest" is a power of two. The worst-case
scenario is when "highest" is one past a power of two (like 65). In that
case, we'll loop until the number is in range. Since all bits have equal
probability of being zero or one, there's a 50-50 chance that the most
significant useful bit will be set[*], in which case we'll need to loop
and we again get the same probability. So on average, we only need two
iterations to get an acceptable result.

[*] There's also a possibility that the other bits are such that the
number is still in range. For 65, we'd need the other 5 bits to be zero
(64 is a valid result), but the probability of that is only 1/2^5 =
3.125%. The bigger "highest" is, the closer we get to zero, so
approximate by saying that never happens and instead calculate that the
most significant useful bit is the controlling one.

[ChangeLog][QtCore][QRandomGenerator] Added 64-bit versions of the
bounded() functions. They are useful in conjunction with Qt 6's 64-bit
container sizes, so code that used to call bounded(list.size()) in Qt 5
will continue to compile and work in Qt 6.

Fixes: QTBUG-86318
Change-Id: I3eb349b832c14610895efffd16356927fe78fd02
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
2020-10-20 22:45:06 -07:00
..
q_func_info CMake: Regenerate projects to use new qt_internal_ API 2020-09-23 16:59:06 +02:00
qflags CMake: Regenerate projects to use new qt_internal_ API 2020-09-23 16:59:06 +02:00
qfloat16 CMake: Regenerate projects to use new qt_internal_ API 2020-09-23 16:59:06 +02:00
qgetputenv CMake: Regenerate projects to use new qt_internal_ API 2020-09-23 16:59:06 +02:00
qglobal CMake: Regenerate projects to use new qt_internal_ API 2020-09-23 16:59:06 +02:00
qglobalstatic CMake: Regenerate projects to use new qt_internal_ API 2020-09-23 16:59:06 +02:00
qhooks CMake: Regenerate projects to use new qt_internal_ API 2020-09-23 16:59:06 +02:00
qkeycombination CMake: Regenerate projects to use new qt_internal_ API 2020-09-23 16:59:06 +02:00
qlogging CMake: Regenerate projects to use new qt_internal_ API 2020-09-23 16:59:06 +02:00
qnumeric Add {add,sub,mul}_overflow detection when one operand is a constant 2020-09-29 14:55:48 -07:00
qrandomgenerator QRandomGenerator: add 64-bit bounded() versions 2020-10-20 22:45:06 -07:00
qtendian CMake: Regenerate projects to use new qt_internal_ API 2020-09-23 16:59:06 +02:00
qwinregistry CMake: Regenerate projects to use new qt_internal_ API 2020-09-23 16:59:06 +02:00
CMakeLists.txt CMake: Regenerate projects 2020-09-22 19:08:53 +02:00
global.pro Long live QKeyCombination! 2020-09-03 07:00:31 +02:00