summaryrefslogtreecommitdiffstats
path: root/src/corelib/global/qrandom.h
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2017-10-23 19:16:34 -0700
committerThiago Macieira <thiago.macieira@intel.com>2017-11-11 08:11:07 +0000
commit08bf28de03f16e5b014b23e228dfc3cfc2ac7feb (patch)
tree0919b98368e7f6ed8faf7bb2fa71b1d711d09a27 /src/corelib/global/qrandom.h
parentcfad4e298f4d65fe26c3a4109d19839bdfcd30c2 (diff)
QRandomGenerator: add more of the std Random Engine API
This brings us to almost parity with the C++11 Random Engine API requirements (see chapter 26.5.1.4 [rand.req.eng]). We don't implement the templated Sseq requirements because it would require moving the implementation details to the public API. And we don't implement the <iostreams> code because we don't want to. Change-Id: Icaa86fc7b54d4b368c0efffd14f05ff813ebd759 Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Diffstat (limited to 'src/corelib/global/qrandom.h')
-rw-r--r--src/corelib/global/qrandom.h24
1 files changed, 20 insertions, 4 deletions
diff --git a/src/corelib/global/qrandom.h b/src/corelib/global/qrandom.h
index c25a0ad643..c31c9afddb 100644
--- a/src/corelib/global/qrandom.h
+++ b/src/corelib/global/qrandom.h
@@ -52,8 +52,8 @@ class QRandomGenerator
template <typename UInt> using IfValidUInt =
typename std::enable_if<std::is_unsigned<UInt>::value && sizeof(UInt) >= sizeof(uint), bool>::type;
public:
- QRandomGenerator(quint32 seed = 1)
- : QRandomGenerator(&seed, 1)
+ QRandomGenerator(quint32 seedValue = 1)
+ : QRandomGenerator(&seedValue, 1)
{}
template <qssize_t N> QRandomGenerator(const quint32 (&seedBuffer)[N])
: QRandomGenerator(seedBuffer, seedBuffer + N)
@@ -68,6 +68,12 @@ public:
Q_CORE_EXPORT QRandomGenerator(const QRandomGenerator &other);
Q_CORE_EXPORT QRandomGenerator &operator=(const QRandomGenerator &other);
+ friend Q_CORE_EXPORT bool operator==(const QRandomGenerator &rng1, const QRandomGenerator &rng2);
+ friend bool operator!=(const QRandomGenerator &rng1, const QRandomGenerator &rng2)
+ {
+ return !(rng1 == rng2);
+ }
+
quint32 generate()
{
quint32 ret;
@@ -151,6 +157,9 @@ public:
// API like std:: random engines
typedef quint32 result_type;
result_type operator()() { return generate(); }
+ void seed(quint32 s = 1) { *this = { s }; }
+ void seed(std::seed_seq &sseq) Q_DECL_NOTHROW { *this = { sseq }; }
+ Q_CORE_EXPORT void discard(unsigned long long z);
static Q_DECL_CONSTEXPR result_type min() { return (std::numeric_limits<result_type>::min)(); }
static Q_DECL_CONSTEXPR result_type max() { return (std::numeric_limits<result_type>::max)(); }
@@ -202,8 +211,8 @@ public:
result_type operator()() { return generate64(); }
#ifndef Q_QDOC
- QRandomGenerator64(quint32 seed = 1)
- : QRandomGenerator(seed)
+ QRandomGenerator64(quint32 seedValue = 1)
+ : QRandomGenerator(seedValue)
{}
template <qssize_t N> QRandomGenerator64(const quint32 (&seedBuffer)[N])
: QRandomGenerator(seedBuffer)
@@ -219,6 +228,13 @@ public:
{}
QRandomGenerator64(const QRandomGenerator &other) : QRandomGenerator(other) {}
+ void discard(unsigned long long z)
+ {
+ Q_ASSERT_X(z * 2 > z, "QRandomGenerator64::discard",
+ "Overflow. Are you sure you want to skip over 9 quintillion samples?");
+ QRandomGenerator::discard(z * 2);
+ }
+
static Q_DECL_CONSTEXPR result_type min() { return (std::numeric_limits<result_type>::min)(); }
static Q_DECL_CONSTEXPR result_type max() { return (std::numeric_limits<result_type>::max)(); }
static Q_CORE_EXPORT QRandomGenerator64 *system();