summaryrefslogtreecommitdiffstats
path: root/src/corelib/global/qrandom.h
Commit message (Collapse)AuthorAgeFilesLines
* QRandomGenerator: assert that bounded() calls have correct argumentsThiago Macieira2019-06-281-4/+6
| | | | | | | | Otherwise, the math will fail badly. Documentation improved to reflect reality. Change-Id: I9e3d261ad9bf41cfb2b6fffd159085cd38e3c388 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* qrandom.h: actually #undef min and max instead of using parenthesesThiago Macieira2017-12-281-4/+11
| | | | | | | | | | | | | | Putting parentheses around the call to (std::numeric_limits<T>::min)() works, but the trick cannot apply to the min() function declaration on the same line. So we really need to #undef. I hope no one after the 1990s still needs these macros. Task-number: QTBUG-65414 Change-Id: I39332e0a867442d58082fffd15024f8edb293311 Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@qt.io> Reviewed-by: Lars Knoll <lars.knoll@qt.io> Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
* Improve readability of code that uses the Qt signed size typev5.10.0-rc2Simon Hausmann2017-11-281-5/+5
| | | | | | | | | | | | | | | | | During the container BoF session at the Qt Contributor Summit 2017 the name of the signed size type became a subject of discussion in the context of readability of code using this type and the intention of using it for all length, size and count properties throughout the entire framework in future versions of Qt. This change proposes qsizetype as new name for qssize_t to emphasize the readability of code over POSIX compatibility, the former being potentially more relevant than the latter to the majority of users of Qt. Change-Id: Idb99cb4a8782703c054fa463a9e5af23a918e7f3 Reviewed-by: Samuel Gaist <samuel.gaist@edeltech.ch> Reviewed-by: David Faure <david.faure@kdab.com>
* QRandomGenerator: Enforce the use of 32-bit integers in the engineThiago Macieira2017-11-201-1/+2
| | | | | | | | | | | std::mt19937 is defined as operating on uint_fast32_t, which is usually just a 32-bit integer. That's not the case on 64-bit Linux, where it is actually 64-bit wide, meaning sizeof(std::mt19937) jumps from 2504 to 5000 bytes, with exactly 50% of it filled with zeroes. The seed() function also needs a large zero-extending loop. Change-Id: Icaa86fc7b54d4b368c0efffd14efa911e2a40b44 Reviewed-by: Lars Knoll <lars.knoll@qt.io>
* QRandomGenerator: optimize the global() and system() storageThiago Macieira2017-11-111-7/+7
| | | | | | | | | | | | We store both in a single memory structure, instead of two local statics. By construction, we also ensure that the global PRNG mutex is in a different cacheline from the global PRNG state itself. Finally, we don't store the full system QRandomGenerator, since we only need the type member from it. Change-Id: Icaa86fc7b54d4b368c0efffd14eecc48ff05ec27 Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
* QRandomGenerator: add more of the std Random Engine APIThiago Macieira2017-11-111-4/+20
| | | | | | | | | | | 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>
* QRandomGenerator: add securelySeeded(), to ensure appropriate seedingThiago Macieira2017-11-111-0/+7
| | | | | | | | | | | | | | | Since we don't document how many bytes one needs (it's 2496), it's difficult for the caller to provide just enough data in the seed sequence. Moreover, since std::mt19937 doesn't make it easy to provide the ideal size either, we can't actually write code that operates optimally given a quint32 range either -- we only provide it via std::seed_seq, which is inefficient. However, we can do it internally by passing QRandomGenerator to the std::mersenne_twister_engine constructor, as it's designed to work. Change-Id: Icaa86fc7b54d4b368c0efffd14f0613c10998321 Reviewed-by: Lars Knoll <lars.knoll@qt.io>
* Change QRandomGenerator to have a deterministic modeThiago Macieira2017-11-111-42/+118
| | | | | | | | | Now only QRandomGenerator::system() will access the system-wide RNG, which we document to be cryptographically-safe and possibly backed by a true HWRNG. Everything else just wraps a Mersenne Twister. Change-Id: I0a103569c81b4711a649fffd14ec8cd3469425df Reviewed-by: Lars Knoll <lars.knoll@qt.io>
* QRandomGenerator: add system() and global()Thiago Macieira2017-10-291-4/+5
| | | | | | | | | | | | | | | | | Right now,this does really nothing. This commit is just to allow us to transition the other modules (besides qtbase) to use the syntax that will become the API. I've marked three places to use the system CSPRNG: 1) the QHash seed 2) QUuid 3) QAuthenticator I didn't think the HTTP multipart boundary needed to be cryptographically safe, so I changed that one to the global generator. Change-Id: Ib17dde1a1dbb49a7bba8fffd14ecf1938bd8ff61 Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
* QRandomGenerator: improve floating-point random generationThiago Macieira2017-10-201-2/+10
| | | | | | | | | | | | | | | | | | | | | | | | | The previous version was good, just not optimal. Because the input was an unsigned 64-bit number, compilers needed to generate extra code to deal with HW instructions that only convert 64-bit signed input. And that was useless because a double uniformly distributed from 0 to 1 can only have 53 bits of randomness. The previous implementation did exactly what the Microsoft libstdc++ and libc++ implementations do. In my opinion, those implementations have an imperfect distribution, which is corrected in this commit. In those, all random input bigger than 0x20000000000000 has a different frequency compared to input below that mark. For example, both 0x20000000000000 and 0x20000000000001 produce the same result (4.8828125e-4). What's more, for the libc++ and MSVC implementations, input between 0xfffffffffffff001 and 0xffffffffffffffff results in 1.0 (probability 1 in 2⁵³), even though the Standard is very clear that the result should be strictly less than 1. GCC 7's libstdc++ doesn't have this issue, whereas the versions before would enter an infinite loop. Change-Id: Ib17dde1a1dbb49a7bba8fffd14eced3c375dd2ec Reviewed-by: Lars Knoll <lars.knoll@qt.io> Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
* QRandomGenerator: update API to better nameThiago Macieira2017-09-221-17/+18
| | | | | | | | | | | | "generate" is better than "get", and we already have "generate(it, it)" which uses std::generate(). This changes: - get32() → generate() - get64() → generate64() and QRandomGenerator64::generate() - getReal() → generateDouble() Change-Id: I6e1fe42ae4b742a7b811fffd14e5d7bd69abcdb3 Reviewed-by: Lars Knoll <lars.knoll@qt.io>
* Long live QRandomGeneratorThiago Macieira2017-06-121-0/+154
This class provides a reasonably-secure random number generator that does not need seeding. That is quite unlike qrand(), which requires a seed and is low-quality (definitely not secure). This class is also like std::random_device, but better. It provides an operator() like std::random_device, but unlike that, it also provides a way to fill a buffer with random data, not just one 32-bit quantity. It's also stateless. Finally, it also implements std::seed_seq-like generate(). It obeys the standard requirement of the range (32-bit) but not that of the algorithm (if you wanted that, you'd use std::seed_seq itself). Instead, generate() fills with pure random data. Change-Id: Icd0e0d4b27cb4e5eb892fffd14b4e3ba9ea04da8 Reviewed-by: Lars Knoll <lars.knoll@qt.io>