diff --git a/docs/command-line.md b/docs/command-line.md index 14511db9..f8e80270 100644 --- a/docs/command-line.md +++ b/docs/command-line.md @@ -277,13 +277,20 @@ is that as long as the random seed is fixed, running only some tests ## Specify a seed for the Random Number Generator -
--rng-seed <'time'|number>
+
--rng-seed <'time'|'random-device'|number>
-Sets a seed for the random number generator using ```std::srand()```. -If a number is provided this is used directly as the seed so the random pattern is repeatable. -Alternatively if the keyword ```time``` is provided then the result of calling ```std::time(0)``` is used and so the pattern becomes unpredictable. In some cases, you might need to pass the keyword ```time``` in double quotes instead of single quotes. +Sets the seed for random number generators used by Catch2. These are used +e.g. to shuffle tests when user asks for tests to be in random order. + +Using `time` as the argument asks Catch2 generate the seed through call +to `std::time(nullptr)`. This provides very weak randomness and multiple +runs of the binary can generate the same seed if they are started close +to each other. + +Using `random-device` asks for `std::random_device` to be used instead. +If your implementation provides working `std::random_device`, it should +be preferred to using `time`. Catch2 uses `std::random_device` by default. -In either case the actual value for the seed is printed as part of Catch's output so if an issue is discovered that is sensitive to test ordering the ordering can be reproduced - even if it was originally seeded from ```std::time(0)```. ## Identify framework and version according to the libIdentify standard diff --git a/docs/release-notes.md b/docs/release-notes.md index 7e0f5a8f..56de2448 100644 --- a/docs/release-notes.md +++ b/docs/release-notes.md @@ -131,6 +131,7 @@ new design. * With the exception of the XmlReporter, the outputs of first party reporters should remain the same * New pair of events were added * One obsolete event was removed +* Catch2 generates a random seed if one hasn't been specified by the user ### Improvements @@ -161,6 +162,7 @@ new design. * `--list-*` flags write their output to file specified by the `-o` flag (#2061, #2163) * `Approx::operator()` is now properly `const` * Catch2's internal helper variables no longer use reserved identifiers (#578) +* `--rng-seed` now accepts string `"random-device"` to generate random seed using `std::random_device` ### Fixes diff --git a/src/catch2/catch_config.cpp b/src/catch2/catch_config.cpp index 20303c2a..b79059ec 100644 --- a/src/catch2/catch_config.cpp +++ b/src/catch2/catch_config.cpp @@ -72,7 +72,7 @@ namespace Catch { ShowDurations Config::showDurations() const { return m_data.showDurations; } double Config::minDuration() const { return m_data.minDuration; } TestRunOrder Config::runOrder() const { return m_data.runOrder; } - unsigned int Config::rngSeed() const { return *m_data.rngSeed; } + uint32_t Config::rngSeed() const { return m_data.rngSeed; } UseColour Config::useColour() const { return m_data.useColour; } bool Config::shouldDebugBreak() const { return m_data.shouldDebugBreak; } int Config::abortAfter() const { return m_data.abortAfter; } diff --git a/src/catch2/catch_config.hpp b/src/catch2/catch_config.hpp index 65a50ed8..7b67ba76 100644 --- a/src/catch2/catch_config.hpp +++ b/src/catch2/catch_config.hpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include @@ -34,7 +35,7 @@ namespace Catch { bool libIdentify = false; int abortAfter = -1; - Optional rngSeed; + uint32_t rngSeed = generateRandomSeed(GenerateFrom::Default); bool benchmarkNoAnalysis = false; unsigned int benchmarkSamples = 100; @@ -97,7 +98,7 @@ namespace Catch { ShowDurations showDurations() const override; double minDuration() const override; TestRunOrder runOrder() const override; - unsigned int rngSeed() const override; + uint32_t rngSeed() const override; UseColour useColour() const override; bool shouldDebugBreak() const override; int abortAfter() const override; diff --git a/src/catch2/catch_session.cpp b/src/catch2/catch_session.cpp index 913c7111..e7878117 100644 --- a/src/catch2/catch_session.cpp +++ b/src/catch2/catch_session.cpp @@ -24,7 +24,6 @@ #include #include -#include #include #include @@ -171,10 +170,6 @@ namespace Catch { if( m_startupExceptions ) return 1; - if (!m_configData.rngSeed) { - m_configData.rngSeed = static_cast(std::time(nullptr)); - } - auto result = m_cli.parse( Clara::Args( argc, argv ) ); if( !result ) { config(); diff --git a/src/catch2/interfaces/catch_interfaces_config.hpp b/src/catch2/interfaces/catch_interfaces_config.hpp index 60c4e9be..e40cf702 100644 --- a/src/catch2/interfaces/catch_interfaces_config.hpp +++ b/src/catch2/interfaces/catch_interfaces_config.hpp @@ -73,7 +73,7 @@ namespace Catch { virtual bool hasTestFilters() const = 0; virtual std::vector const& getTestsOrTags() const = 0; virtual TestRunOrder runOrder() const = 0; - virtual unsigned int rngSeed() const = 0; + virtual uint32_t rngSeed() const = 0; virtual UseColour useColour() const = 0; virtual std::vector const& getSectionsToRun() const = 0; virtual Verbosity verbosity() const = 0; diff --git a/src/catch2/internal/catch_commandline.cpp b/src/catch2/internal/catch_commandline.cpp index e5fdd692..ad571cc5 100644 --- a/src/catch2/internal/catch_commandline.cpp +++ b/src/catch2/internal/catch_commandline.cpp @@ -15,7 +15,6 @@ #include #include -#include #include namespace Catch { @@ -74,7 +73,10 @@ namespace Catch { }; auto const setRngSeed = [&]( std::string const& seed ) { if( seed == "time" ) { - config.rngSeed = static_cast(std::time(nullptr)); + config.rngSeed = generateRandomSeed(GenerateFrom::Time); + return ParserResult::ok(ParseResultType::Matched); + } else if (seed == "random-device") { + config.rngSeed = generateRandomSeed(GenerateFrom::RandomDevice); return ParserResult::ok(ParseResultType::Matched); } @@ -211,7 +213,7 @@ namespace Catch { | Opt( setTestOrder, "decl|lex|rand" ) ["--order"] ( "test case order (defaults to decl)" ) - | Opt( setRngSeed, "'time'|number" ) + | Opt( setRngSeed, "'time'|'random-device'|number" ) ["--rng-seed"] ( "set a specific seed for random numbers" ) | Opt( setColourUsage, "yes|no" )