24 template <
typename UInt>
using IfValidUInt =
25 typename std::enable_if<std::is_unsigned<UInt>::value &&
sizeof(UInt) >=
sizeof(uint),
bool>::type;
27 QRandomGenerator(quint32 seedValue = 1)
28 : QRandomGenerator(&seedValue, 1)
30 template <qsizetype N> QRandomGenerator(
const quint32 (&seedBuffer)[N])
31 : QRandomGenerator(seedBuffer, seedBuffer + N)
33 QRandomGenerator(
const quint32 *seedBuffer, qsizetype len)
34 : QRandomGenerator(seedBuffer, seedBuffer + len)
36 Q_CORE_EXPORT QRandomGenerator(std::seed_seq &sseq)
noexcept;
37 Q_CORE_EXPORT QRandomGenerator(
const quint32 *begin,
const quint32 *end);
40 Q_CORE_EXPORT QRandomGenerator(
const QRandomGenerator &other);
41 Q_CORE_EXPORT QRandomGenerator &operator=(
const QRandomGenerator &other);
43 ~QRandomGenerator() =
default;
45 friend Q_CORE_EXPORT
bool operator==(
const QRandomGenerator &rng1,
const QRandomGenerator &rng2);
46 friend bool operator!=(
const QRandomGenerator &rng1,
const QRandomGenerator &rng2)
48 return !(rng1 == rng2);
53 return quint32(_fillRange(
nullptr, 1));
58 return _fillRange(
nullptr,
sizeof(quint64) /
sizeof(quint32));
61 double generateDouble()
69 quint64 x = generate64();
70 quint64 limit = Q_UINT64_C(1) << std::numeric_limits<
double>::digits;
71 x >>= std::numeric_limits<quint64>::digits - std::numeric_limits<
double>::digits;
72 return double(x) /
double(limit);
75 double bounded(
double highest)
77 return generateDouble() * highest;
80 quint32 bounded(quint32 highest)
82 quint64 value = generate();
84 value /= (max)() + quint64(1);
85 return quint32(value);
88 quint32 bounded(quint32 lowest, quint32 highest)
90 Q_ASSERT(highest > lowest);
91 return bounded(highest - lowest) + lowest;
94 int bounded(
int highest)
96 Q_ASSERT(highest > 0);
97 return int(bounded(0U, quint32(highest)));
100 int bounded(
int lowest,
int highest)
102 return bounded(highest - lowest) + lowest;
105 quint64 bounded(quint64 highest);
107 quint64 bounded(quint64 lowest, quint64 highest)
109 Q_ASSERT(highest > lowest);
110 return bounded(highest - lowest) + lowest;
113 qint64 bounded(qint64 highest)
115 Q_ASSERT(highest > 0);
116 return qint64(bounded(quint64(0), quint64(highest)));
119 qint64 bounded(qint64 lowest, qint64 highest)
121 return bounded(highest - lowest) + lowest;
125 qint64 bounded(
int lowest, qint64 highest)
127 return bounded(qint64(lowest), qint64(highest));
129 qint64 bounded(qint64 lowest,
int highest)
131 return bounded(qint64(lowest), qint64(highest));
134 quint64 bounded(
unsigned lowest, quint64 highest)
136 return bounded(quint64(lowest), quint64(highest));
138 quint64 bounded(quint64 lowest,
unsigned highest)
140 return bounded(quint64(lowest), quint64(highest));
143 template <
typename UInt, IfValidUInt<UInt> =
true>
144 void fillRange(UInt *buffer, qsizetype count)
146 _fillRange(buffer, count *
sizeof(UInt) /
sizeof(quint32));
149 template <
typename UInt, size_t N, IfValidUInt<UInt> =
true>
150 void fillRange(UInt (&buffer)[N])
152 _fillRange(buffer, N *
sizeof(UInt) /
sizeof(quint32));
156 template <
typename ForwardIterator>
157 void generate(ForwardIterator begin, ForwardIterator end)
159 std::generate(begin, end, [
this]() {
return generate(); });
162 void generate(quint32 *begin, quint32 *end)
164 _fillRange(begin, end - begin);
168 typedef quint32 result_type;
169 result_type operator()() {
return generate(); }
170 void seed(quint32 s = 1) { *
this = { s }; }
171 void seed(std::seed_seq &sseq)
noexcept { *
this = { sseq }; }
172 Q_CORE_EXPORT
void discard(
unsigned long long z);
173 static constexpr result_type min() {
return (std::numeric_limits<result_type>::min)(); }
174 static constexpr result_type max() {
return (std::numeric_limits<result_type>::max)(); }
176 static inline Q_DECL_CONST_FUNCTION QRandomGenerator *system();
177 static inline Q_DECL_CONST_FUNCTION QRandomGenerator *global();
178 static inline QRandomGenerator securelySeeded();
182 QRandomGenerator(System);
185 Q_CORE_EXPORT quint64 _fillRange(
void *buffer, qptrdiff count);
187 struct InitialRandomData {
188 quintptr data[16 /
sizeof(quintptr)];
190 friend InitialRandomData qt_initial_random_value()
noexcept;
191 friend class QRandomGenerator64;
192 struct SystemGenerator;
193 struct SystemAndGlobalGenerators;
194 using RandomEngine = std::mersenne_twister_engine<quint32,
195 32,624,397,31,0x9908b0df,11,0xffffffff,7,0x9d2c5680,15,0xefc60000,18,1812433253>;
199 RandomEngine twister;
200 RandomEngine &engine() {
return twister; }
201 const RandomEngine &engine()
const {
return twister; }
203 static_assert(std::is_trivially_destructible<RandomEngine>::value,
204 "std::mersenne_twister not trivially destructible as expected");