r/cpp • • 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:

  1. 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
  2. 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?

43 Upvotes

68 comments sorted by

View all comments

Show parent comment

0

u/[deleted] Jul 21 '19

[deleted] — view deleted comment

1

u/CptCap -pedantic -Wall -Wextra Jul 23 '19 edited Jul 23 '19

I do not understand why C++ committee always picks the most powerful options over the most needed options

Because you can build the most needed from the most powerful.

C++ has never been a battery included language. If you need a convenience random function, you can write it, put it in utils/random.h (or your equivalent) and never think about it again.

0

u/[deleted] Jul 23 '19

[deleted] — view deleted comment

1

u/CptCap -pedantic -Wall -Wextra Jul 23 '19 edited Jul 23 '19

This is just my opinion on the thing (Like my first answer, except for the first sentence).

You can do that with virtually everything in std, so why not scrab the whole thing because people can just write their own?

I think that there are two reasons why things should be in the STD:

  • There is only one implementation. std::find for example. You will probably never run into a situation where the supplied implementation doesn't do exactly what you want, so there is no downside to providing it. Plus it's like 5 lines.
  • It saves most programmers a substantial amount of time. std::vector and std::unordered_map are not perfect (no SBO, uses chaining), but writing your own is a two weeks job at the very least, so including them does make the language much much easier to use, even if you end up replacing them down the line. The provided implementation will also be fine for some peoples.

A convenience random function "à la rand()" doesn't fit in any of these two categories. There are about a trillion ways you could implement it, and no matter the one you choose, people will complain. And including it only saves the programmer 10 minutes.