This revision was automatically updated to reflect the committed changes. Closed by commit rL317689: [clang-tidy] Add a note about modernize-replace-random-shuffle (authored by krasimir).
Repository: rL LLVM https://reviews.llvm.org/D39787 Files: clang-tools-extra/trunk/docs/clang-tidy/checks/modernize-replace-random-shuffle.rst Index: clang-tools-extra/trunk/docs/clang-tidy/checks/modernize-replace-random-shuffle.rst =================================================================== --- clang-tools-extra/trunk/docs/clang-tidy/checks/modernize-replace-random-shuffle.rst +++ clang-tools-extra/trunk/docs/clang-tidy/checks/modernize-replace-random-shuffle.rst @@ -26,3 +26,16 @@ The second example will also receive a warning that ``randomFunc`` is no longer supported in the same way as before so if the user wants the same functionality, the user will need to change the implementation of the ``randomFunc``. One thing to be aware of here is that ``std::random_device`` is quite expensive to initialize. So if you are using the code in a performance critical place, you probably want to initialize it elsewhere. +Another thing is that the seeding quality of the suggested fix is quite poor: ``std::mt19937`` has an internal state of 624 32-bit integers, but is only seeded with a single integer. So if you require +higher quality randomness, you should consider seeding better, for example: + +.. code-block:: c++ + + std::shuffle(v.begin(), v.end(), []() { + std::mt19937::result_type seeds[std::mt19937::state_size]; + std::random_device device; + std::uniform_int_distribution<typename std::mt19937::result_type> dist; + std::generate(std::begin(seeds), std::end(seeds), [&] { return dist(device); }); + std::seed_seq seq(std::begin(seeds), std::end(seeds)); + return std::mt19937(seq); + }());
Index: clang-tools-extra/trunk/docs/clang-tidy/checks/modernize-replace-random-shuffle.rst =================================================================== --- clang-tools-extra/trunk/docs/clang-tidy/checks/modernize-replace-random-shuffle.rst +++ clang-tools-extra/trunk/docs/clang-tidy/checks/modernize-replace-random-shuffle.rst @@ -26,3 +26,16 @@ The second example will also receive a warning that ``randomFunc`` is no longer supported in the same way as before so if the user wants the same functionality, the user will need to change the implementation of the ``randomFunc``. One thing to be aware of here is that ``std::random_device`` is quite expensive to initialize. So if you are using the code in a performance critical place, you probably want to initialize it elsewhere. +Another thing is that the seeding quality of the suggested fix is quite poor: ``std::mt19937`` has an internal state of 624 32-bit integers, but is only seeded with a single integer. So if you require +higher quality randomness, you should consider seeding better, for example: + +.. code-block:: c++ + + std::shuffle(v.begin(), v.end(), []() { + std::mt19937::result_type seeds[std::mt19937::state_size]; + std::random_device device; + std::uniform_int_distribution<typename std::mt19937::result_type> dist; + std::generate(std::begin(seeds), std::end(seeds), [&] { return dist(device); }); + std::seed_seq seq(std::begin(seeds), std::end(seeds)); + return std::mt19937(seq); + }());
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits