From 28e83b32ea767fc5ddd35e8037709de7409ed591 Mon Sep 17 00:00:00 2001 From: oo01oo <160168196+oo01oo@users.noreply.github.com> Date: Thu, 15 Feb 2024 22:51:33 +0400 Subject: [PATCH] Update index.md to wrap PRNG in std::ref for std::bind calls To avoid problems with the quality of randomness, it is better for the distribution to be bound to a reference to the engine rather than a copy. This change adds an include for , which appears missing. --- content/languages/cpp/authoring/index.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/content/languages/cpp/authoring/index.md b/content/languages/cpp/authoring/index.md index 3291555a..b44e5399 100644 --- a/content/languages/cpp/authoring/index.md +++ b/content/languages/cpp/authoring/index.md @@ -196,8 +196,8 @@ std::uniform_int_distribution< char> rand_bool { 0, 1 }; // etc. // for convenience, distributions can be bound with PRNG -auto gen_length = std::bind(rand_length, engine); -auto gen_letter = std::bind(rand_letter, engine); +auto gen_length = std::bind(rand_length, std::ref(engine)); +auto gen_letter = std::bind(rand_letter, std::ref(engine)); std::string input; // input string generator: string of random length, composed of random letters @@ -281,6 +281,7 @@ Below you can find an example test suite that covers most of the common scenario #include #include #include +#include #include Describe(FixedTests) { @@ -315,9 +316,9 @@ Describe(RandomTests) { private: std::mt19937 engine{ std::random_device{}() }; - std::function gen_number = std::bind(std::uniform_int_distribution{ 1, 100 }, engine); - std::function gen_small_size = std::bind(std::uniform_int_distribution{ 2, 10 }, engine); - std::function gen_large_size = std::bind(std::uniform_int_distribution{ 80, 100 }, engine); + std::function gen_number = std::bind(std::uniform_int_distribution{ 1, 100 }, std::ref(engine)); + std::function gen_small_size = std::bind(std::uniform_int_distribution{ 2, 10 }, std::ref(engine)); + std::function gen_large_size = std::bind(std::uniform_int_distribution{ 80, 100 }, std::ref(engine)); // random test case generator std::vector generate_random_input(size_t size) {