mariusbancila / stduuid

A C++17 cross-platform implementation for UUIDs
MIT License
742 stars 112 forks source link

Recommend&support direct use of std::random_device #42

Closed NorbertNemec closed 2 years ago

NorbertNemec commented 3 years ago

The examples in the README show the generation of a single UUID by using std::random_device to seed a mt19937 and then use that for generating a UUID. While mt19937 is a reasonable intermediate step for generating a sequence of UUIDs, it is a pointless waste of entropy and computing time when generating a single UUID.

For a single-shot UUID generator, the following should be recommended:

        std::random_device rd;
        uuids::basic_uuid_random_generator<std::random_device> gen{rd};

Since single-shot UUID generation is a common pattern, one might want to consider deriving a specialized class (e.g. uuid_random_device_generator to do just this.

It might be worth adding a disclaimer that std::random_device may not be properly supported on all systems, as discussed in https://github.com/mariusbancila/stduuid/issues/6

mariusbancila commented 2 years ago

Disclaimer and note about mingw added to the readme.

NorbertNemec commented 2 years ago

The added comment is valid but completely misses the point I made:

If random_device is implemented deterministically using a pseudo RNG, feeding it through mt19937 will not improve the situation.

If random_device is implemented using a proper entropy source, feeding it through mt19937 for a single-shot use is a waste of entropy without any gain.

Either way, the code in the example is suboptimal and unnecessarily complex. Using random_device to seed an mt19937 for a single-shot use is never a good idea.