summaryrefslogtreecommitdiffstats
path: root/src/corelib
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2019-03-29 12:49:47 -0700
committerThiago Macieira <thiago.macieira@intel.com>2019-06-28 23:05:57 -0700
commit5d7e221bbf9e27e90448243909abc76d81733381 (patch)
tree0165b5ef03eba0fb2c7dc32562522974c4c7a218 /src/corelib
parent57eed823e412d587e051bff0d4775f08024d4169 (diff)
QRandomGenerator: assert that bounded() calls have correct arguments
Otherwise, the math will fail badly. Documentation improved to reflect reality. Change-Id: I9e3d261ad9bf41cfb2b6fffd159085cd38e3c388 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib')
-rw-r--r--src/corelib/global/qrandom.cpp16
-rw-r--r--src/corelib/global/qrandom.h10
2 files changed, 18 insertions, 8 deletions
diff --git a/src/corelib/global/qrandom.cpp b/src/corelib/global/qrandom.cpp
index 90df8653a7..bf01b7ae2a 100644
--- a/src/corelib/global/qrandom.cpp
+++ b/src/corelib/global/qrandom.cpp
@@ -903,6 +903,10 @@ inline QRandomGenerator::SystemGenerator &QRandomGenerator::SystemGenerator::sel
\snippet code/src_corelib_global_qrandom.cpp 12
+ If the \a 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).
+
\sa generateDouble(), bounded()
*/
@@ -934,7 +938,7 @@ inline QRandomGenerator::SystemGenerator &QRandomGenerator::SystemGenerator::sel
\overload
Generates one random 32-bit quantity in the range between 0 (inclusive) and
- \a highest (exclusive). \a highest must not be negative.
+ \a highest (exclusive). \a 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.
@@ -946,8 +950,11 @@ inline QRandomGenerator::SystemGenerator &QRandomGenerator::SystemGenerator::sel
\fn quint32 QRandomGenerator::bounded(quint32 lowest, quint32 highest)
\overload
- Generates one random 32-bit quantity in the range between \a lowest (inclusive)
- and \a highest (exclusive). The same result may also be obtained by using
+ Generates one random 32-bit quantity in the range between \a lowest
+ (inclusive) and \a highest (exclusive). The \a highest parameter must be
+ greater than \a lowest.
+
+ The same result may also be obtained by using
\l{http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution}{\c std::uniform_int_distribution}
with parameters \a lowest and \c{\a highest - 1}. That class can also be used to
obtain quantities larger than 32 bits.
@@ -968,7 +975,8 @@ inline QRandomGenerator::SystemGenerator &QRandomGenerator::SystemGenerator::sel
\overload
Generates one random 32-bit quantity in the range between \a lowest
- (inclusive) and \a highest (exclusive), both of which may be negative.
+ (inclusive) and \a highest (exclusive), both of which may be negative, but
+ \a highest must be greater than \a 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.
diff --git a/src/corelib/global/qrandom.h b/src/corelib/global/qrandom.h
index 46d3e0e152..260fc1501a 100644
--- a/src/corelib/global/qrandom.h
+++ b/src/corelib/global/qrandom.h
@@ -122,14 +122,16 @@ public:
return quint32(value);
}
- int bounded(int highest)
+ quint32 bounded(quint32 lowest, quint32 highest)
{
- return int(bounded(quint32(highest)));
+ Q_ASSERT(highest > lowest);
+ return bounded(highest - lowest) + lowest;
}
- quint32 bounded(quint32 lowest, quint32 highest)
+ int bounded(int highest)
{
- return bounded(highest - lowest) + lowest;
+ Q_ASSERT(highest > 0);
+ return int(bounded(0U, quint32(highest)));
}
int bounded(int lowest, int highest)