From a9890afd12bd3bf41a092bf9d8e822408b4fe1c2 Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Thu, 13 Jun 2019 14:01:44 -0700 Subject: [PATCH] Fix text2image compilation on C++17 compilers C++17 drops support for `std::random_shuffle`, breaking C++17 compilers that run to compile text2image.cpp. std::shuffle is valid on C++11 through C++17, so use std::shuffle instead. Due to the use `std::random_shuffle`, `text2image --render_ngrams` would not give consistent results for different compilers or platforms. With the current change, the same random number generator is used for all platforms and initialized to the same seed, so training output should be consistent. --- src/training/text2image.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/training/text2image.cpp b/src/training/text2image.cpp index ab0b3f733a..45b7b09a25 100644 --- a/src/training/text2image.cpp +++ b/src/training/text2image.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -573,8 +574,11 @@ static int Main() { offset += step; offset += SpanUTF8Whitespace(str8 + offset); } - if (FLAGS_render_ngrams) - std::random_shuffle(offsets.begin(), offsets.end()); + if (FLAGS_render_ngrams) { + std::seed_seq seed{kRandomSeed}; + std::mt19937 random_gen(seed); + std::shuffle(offsets.begin(), offsets.end(), random_gen); + } for (size_t i = 0, line = 1; i < offsets.size(); ++i) { const char *curr_pos = str8 + offsets[i].first;