diff --git a/Applications/FileIO/Legacy/createSurface.cpp b/Applications/FileIO/Legacy/createSurface.cpp index f89812667d34c20c684d3ebbb6e03f3ee11f315f..69698a3cc65347632082a5220b1f3220f88c292a 100644 --- a/Applications/FileIO/Legacy/createSurface.cpp +++ b/Applications/FileIO/Legacy/createSurface.cpp @@ -81,8 +81,8 @@ bool createSurface(GeoLib::Polyline const& ply, gmsh_io.setPrecision(std::numeric_limits<double>::digits10); // write to random file in temp directory - auto geo_file = fs::temp_directory_path() /= BaseLib::random_string(32); - auto msh_file = fs::temp_directory_path() /= BaseLib::random_string(32); + auto geo_file = fs::temp_directory_path() /= BaseLib::randomString(32); + auto msh_file = fs::temp_directory_path() /= BaseLib::randomString(32); gmsh_io.writeToFile(geo_file.string()); // Newer gmsh versions write a newer file format for meshes per default. At diff --git a/BaseLib/StringTools.cpp b/BaseLib/StringTools.cpp index bc041110854a08416ad504710e59416cac2d9e1c..4faa7ee99099187b88bf4a2c7f7d1c624073c021 100644 --- a/BaseLib/StringTools.cpp +++ b/BaseLib/StringTools.cpp @@ -15,13 +15,14 @@ #include "StringTools.h" #include <algorithm> +#include <boost/algorithm/string/replace.hpp> #include <cctype> +#include <chrono> #include <cstdarg> #include <cstdio> #include <iomanip> - #include <logog/include/logog.hpp> -#include <boost/algorithm/string/replace.hpp> +#include <random> namespace BaseLib { @@ -104,21 +105,24 @@ std::string format(const char* format_str, ... ) return std::string(buffer.data()); } -std::string random_string( size_t length ) +std::string randomString(std::size_t const length) { - auto randchar = []() -> char - { - const char charset[] = + static constexpr char charset[] = "0123456789" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz"; - const size_t max_index = (sizeof(charset) - 1); - return charset[ rand() % max_index ]; - }; - std::string str(length,0); - std::generate_n( str.begin(), length, randchar ); - return str; + static const auto seed = static_cast<std::mt19937::result_type>( + std::chrono::system_clock::now().time_since_epoch().count()); + static std::mt19937 generator{seed}; + static std::uniform_int_distribution<unsigned short> distribution( + 0, sizeof(charset) - 2); + + std::string s(length, 0); + std::generate_n( + begin(s), length, [&]() { return charset[distribution(generator)]; }); + + return s; } } // end namespace BaseLib diff --git a/BaseLib/StringTools.h b/BaseLib/StringTools.h index 27f6f8a6f327aaa2c8a351c77d93756c05d03676..ebaa82e523b920d6b4b67db9ccff7407bf834d2d 100644 --- a/BaseLib/StringTools.h +++ b/BaseLib/StringTools.h @@ -84,6 +84,6 @@ std::string const& tostring(std::string const& value); std::string format(const char* format_string, ... ); //! Returns a random string of the given length containing just a-z,A-Z,0-9 -std::string random_string( size_t length ); +std::string randomString(std::size_t length); } // end namespace BaseLib diff --git a/Tests/FileIO/TestBoostGmlInterface.cpp b/Tests/FileIO/TestBoostGmlInterface.cpp index 28ef94f5ce22ab0e66853a30e0cb8f2a31dc26ed..acdeca0fd1ef298c32ba3a9d1b62a1d917749279 100644 --- a/Tests/FileIO/TestBoostGmlInterface.cpp +++ b/Tests/FileIO/TestBoostGmlInterface.cpp @@ -27,7 +27,7 @@ TEST_F(TestGmlInterface, BoostXmlGmlWriterReaderTest) { // Writer test std::string test_data_file = - (fs::temp_directory_path() /= BaseLib::random_string(32) + ".gml").string(); + (fs::temp_directory_path() /= BaseLib::randomString(32) + ".gml").string(); GeoLib::IO::BoostXmlGmlInterface xml(geo_objects); xml.setNameForExport(geo_name); diff --git a/Tests/FileIO/TestCsvReader.cpp b/Tests/FileIO/TestCsvReader.cpp index 7f7bb2c720cca8c44ad867dd212006fee28cc6d7..01abc8460dd282e29dc9c3c4a8d097721fc84fde 100644 --- a/Tests/FileIO/TestCsvReader.cpp +++ b/Tests/FileIO/TestCsvReader.cpp @@ -27,7 +27,7 @@ public: CsvInterfaceTest() { _file_name = - (fs::temp_directory_path() /= BaseLib::random_string(32) + ".csv").string(); + (fs::temp_directory_path() /= BaseLib::randomString(32) + ".csv").string(); std::ofstream out(_file_name); out << "id\tx\ty\tz\tname\tvalue1\tvalue_two\n"; out << "0\t642015.538\t5724666.445\t391.759\ttest_a\t11.05303121\t436.913\t133\n"; diff --git a/Tests/FileIO/TestCsvWriter.cpp b/Tests/FileIO/TestCsvWriter.cpp index 3c5c3340e0b6103a5d4fdb635075a8efeba4f8e9..1cac6c014617f3f0842ec0b85ecc31cc071183a6 100644 --- a/Tests/FileIO/TestCsvWriter.cpp +++ b/Tests/FileIO/TestCsvWriter.cpp @@ -22,7 +22,7 @@ TEST(CsvWriter, WriteReadTest) { std::string test_file( - (fs::temp_directory_path() /= BaseLib::random_string(32)).string()); + (fs::temp_directory_path() /= BaseLib::randomString(32)).string()); std::vector<std::string> str_vec {"Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet" }; std::vector<int> int_vec { 1, 2, 4, 8, 16, 32, 64 }; diff --git a/Tests/FileIO/TestTetGenInterface.cpp b/Tests/FileIO/TestTetGenInterface.cpp index ae915556f966c10af0c69eebbaf063677e82938a..57ab6981516147377f389250f9dd2eca64661780 100644 --- a/Tests/FileIO/TestTetGenInterface.cpp +++ b/Tests/FileIO/TestTetGenInterface.cpp @@ -55,7 +55,7 @@ TEST(FileIO, DISABLED_TetGenSmeshInterface) std::string const file_name (TestInfoLib::TestInfo::data_path + "/FileIO/AmmerSubsurfaceCoarse.vtu"); std::unique_ptr<MeshLib::Mesh const> const mesh (MeshLib::IO::readMeshFromFile(file_name)); - std::string const tg_new_name (BaseLib::random_string(32)); + std::string const tg_new_name (BaseLib::randomString(32)); std::string const output_name = (fs::temp_directory_path() /= tg_new_name + ".smesh").string(); std::cout << output_name << std::endl; diff --git a/Tests/FileIO_Qt/TestQtGmlInterface.cpp b/Tests/FileIO_Qt/TestQtGmlInterface.cpp index 2fe80964556297d040749b7baf4e5018fabe45ad..bf361f4e7af36506ba80617219b003bd243ac2cc 100644 --- a/Tests/FileIO_Qt/TestQtGmlInterface.cpp +++ b/Tests/FileIO_Qt/TestQtGmlInterface.cpp @@ -27,7 +27,7 @@ TEST_F(TestGmlInterface, QtXmlGmlWriterReaderTest) { // Writer test std::string test_data_file = - (fs::temp_directory_path() /= BaseLib::random_string(32)).string(); + (fs::temp_directory_path() /= BaseLib::randomString(32)).string(); GeoLib::IO::XmlGmlInterface xml(geo_objects); xml.setNameForExport(geo_name); diff --git a/Tests/GeoLib/IO/TestGLIReader.cpp b/Tests/GeoLib/IO/TestGLIReader.cpp index 331bc924eab41b35c062e9da87872615e2b282ee..d71c3f1aa47864065e82de07b3f65cc6dbddc482 100644 --- a/Tests/GeoLib/IO/TestGLIReader.cpp +++ b/Tests/GeoLib/IO/TestGLIReader.cpp @@ -24,7 +24,7 @@ class OGSIOVer4InterfaceTest : public ::testing::Test { public: OGSIOVer4InterfaceTest() - : _test_path(fs::temp_directory_path() /= BaseLib::random_string(32)), + : _test_path(fs::temp_directory_path() /= BaseLib::randomString(32)), _gli_fname(_test_path), _surface_fname(_test_path) { fs::create_directory(_test_path); diff --git a/Tests/MeshLib/TestVtkMappedMeshSource.cpp b/Tests/MeshLib/TestVtkMappedMeshSource.cpp index bd00c820b455259fd2f50a3dbf7d75059b9a7bd2..1c8d8320a17de39ae8d6209ca74d4b2cc2f4f566 100644 --- a/Tests/MeshLib/TestVtkMappedMeshSource.cpp +++ b/Tests/MeshLib/TestVtkMappedMeshSource.cpp @@ -153,7 +153,7 @@ TEST_F(InSituMesh, DISABLED_MappedMeshSourceRoundtrip) ASSERT_TRUE(mesh != nullptr); std::string test_data_file = - (fs::temp_directory_path() /= BaseLib::random_string(32)).string(); + (fs::temp_directory_path() /= BaseLib::randomString(32)).string(); // -- Test VtkMappedMeshSource, i.e. OGS mesh to VTK mesh vtkNew<MeshLib::VtkMappedMeshSource> vtkSource;