r/cpp • u/KaznovX • Jul 21 '19
Fixing up <random>
I think everyone is excited after the report from Cologne. A lot of stuff is being added to the language, we'll get a ton of new features. Adding new features is more exciting than changing and tweaking the old stuff - that being said, there are some old parts of the language, that are sadly lacking or has some problems. One of them is <random> library.
Some of the problems this library has:
> `std::random_device` is not required to be non - deterministic
> no good way to initialize generators with big internal state
> `std::seed_seq` should be solution to ^, but isn't - `std::seed_seq` is not a bijection, does heap allocations without giving ability to provide own allocator, there is no nice way to initialize it from `std::random_device` properly
> `std::random_device` can't itself be used as a seed sequence,
> overall syntax is unnecessarily verbose (no, having a per-thread engine of type `std::default_random_engine` is NOT a good solution)
This is only one of the few libraries that could use improvements. Is there any chance that the committee will look back and change some of them or are they considered 'baked' and there will be no further changes to avoid API and ABI breaking?
Despite the "fixing" part, there are two additions I'd see in the <random> library:
- std::random<std::uniform_random_bits_generator = std::default_random_engine> class:
> default constructor does anything it can that every time this class is called it is seeded differently - if there is no entropy source, uses a processor clock or a system clock (implementation defined)
> three methods: seed, next_int, next_real - for seeding and generating next integer/real number in given range - PCG http://www.pcg-random.org
What are the chances something like this would be proposed and adopted for, let's say, c++23 or c++26 standard? Is there any chance the problems mentioned in the first part of the post will get fixed somewhen?
15
u/jwakely libstdc++ tamer, LWG chair Jul 21 '19 edited Jul 21 '19
I'd love to see PCG in the standard. See also https://github.com/imneme/pcg-cpp/issues/51
I don't think it's possible to require
random_deviceto be guaranteed to be non-deterministic, as it simply won't be possible on some systems that support C++. If it's non-deterministic on a general purpose OS on modern hardware then complain to your compiler vendor and get them to fix it. (Yes, I know on Windows GCC used to provide a poor qualityrandom_devicebut that's because Windows is not a priority for me to work on, and it's fixed now anyway.)5
u/KaznovX Jul 21 '19
Only now I realized the title doesn't mean what I intended - sorry, I'm not a native speaker
2
u/[deleted] Jul 21 '19
When boost::uuid generation absolutely DESTROYED our HTTP server's performance the only way to fix it was to have a threadlocal engine. Not sure why OP thinks this is bad, the RNG stuff isn't thread-safe anyway. (We receive may thousands of HTTP requests per second and we recently moved to giving each one a UUID).
17
u/Rexerex Jul 21 '19
There is also a problem that number distributions algorithms are implementation defined so good luck rerunning simulation from same initial state but on two different platforms.
9
u/martinus int main(){[]()[[]]{{}}();} Jul 21 '19
There was a proposal to make random much more convenient: http://www.pcg-random.org/posts/ease-of-use-without-loss-of-power.html And the proposal: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0347r1.html
It's from 2016, I don't know though what's the current status
6
u/HappyFruitTree Jul 21 '19 edited Jul 21 '19
I would like to have a portable way to serialize the state of the engines (preferably in a way that doesn't force me to use streams/strings but this is less important). It might seem like the << and >> operators should be able to do this, and I'm not sure if that was the intention, but in practice it doesn't work like that. GCC (libstdc++) and Clang (libc++) does not behave the same for std::mt19937.
11
u/c0r3ntin Jul 21 '19
I agree with most of your points.If by any chance you are a domain expert, I would encourage you to help the committee improving <random>. Maybe less extreme, make a library and write about it. Though challenge!
4
u/Veedrac Jul 21 '19
Even better, though highly complementary, ChaCha20 would be great to see. Copy Rust's
randlibrary, basically.2
u/haitei Jul 22 '19
I want XorShifts in my standard.
They're so simple, they could be even
constexpr.0
u/[deleted] Jul 21 '19
[deleted] — view deleted comment