mirror of
https://github.com/catchorg/Catch2.git
synced 2024-11-22 13:26:10 +01:00
Replaced use of std::rand with std::mt19937
This uses a global instance of the RNG
This commit is contained in:
parent
1dce91d78e
commit
35a57b070f
@ -9,23 +9,21 @@
|
|||||||
#include "catch_context.h"
|
#include "catch_context.h"
|
||||||
#include "catch_interfaces_config.h"
|
#include "catch_interfaces_config.h"
|
||||||
|
|
||||||
#include <cstdlib>
|
|
||||||
|
|
||||||
namespace Catch {
|
namespace Catch {
|
||||||
|
|
||||||
void seedRng( IConfig const& config ) {
|
std::mt19937& rng() {
|
||||||
if( config.rngSeed() != 0 )
|
static std::mt19937 s_rng;
|
||||||
std::srand( config.rngSeed() );
|
return s_rng;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void seedRng( IConfig const& config ) {
|
||||||
|
if( config.rngSeed() != 0 ) {
|
||||||
|
std::srand( config.rngSeed() );
|
||||||
|
rng().seed( config.rngSeed() );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
unsigned int rngSeed() {
|
unsigned int rngSeed() {
|
||||||
return getCurrentContext().getConfig()->rngSeed();
|
return getCurrentContext().getConfig()->rngSeed();
|
||||||
}
|
}
|
||||||
|
|
||||||
RandomNumberGenerator::result_type RandomNumberGenerator::operator()( result_type n ) const {
|
|
||||||
return std::rand() % n;
|
|
||||||
}
|
|
||||||
RandomNumberGenerator::result_type RandomNumberGenerator::operator()() const {
|
|
||||||
return std::rand() % (max)();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -8,31 +8,16 @@
|
|||||||
#define TWOBLUECUBES_CATCH_RANDOM_NUMBER_GENERATOR_H_INCLUDED
|
#define TWOBLUECUBES_CATCH_RANDOM_NUMBER_GENERATOR_H_INCLUDED
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <random>
|
||||||
|
|
||||||
namespace Catch {
|
namespace Catch {
|
||||||
|
|
||||||
struct IConfig;
|
struct IConfig;
|
||||||
|
|
||||||
|
std::mt19937& rng();
|
||||||
void seedRng( IConfig const& config );
|
void seedRng( IConfig const& config );
|
||||||
|
|
||||||
unsigned int rngSeed();
|
unsigned int rngSeed();
|
||||||
|
|
||||||
struct RandomNumberGenerator {
|
|
||||||
using result_type = unsigned int;
|
|
||||||
|
|
||||||
static constexpr result_type (min)() { return 0; }
|
|
||||||
static constexpr result_type (max)() { return 1000000; }
|
|
||||||
|
|
||||||
result_type operator()( result_type n ) const;
|
|
||||||
result_type operator()() const;
|
|
||||||
|
|
||||||
template<typename V>
|
|
||||||
static void shuffle( V& vector ) {
|
|
||||||
RandomNumberGenerator rng;
|
|
||||||
std::shuffle( vector.begin(), vector.end(), rng );
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // TWOBLUECUBES_CATCH_RANDOM_NUMBER_GENERATOR_H_INCLUDED
|
#endif // TWOBLUECUBES_CATCH_RANDOM_NUMBER_GENERATOR_H_INCLUDED
|
||||||
|
@ -28,7 +28,7 @@ namespace Catch {
|
|||||||
break;
|
break;
|
||||||
case RunTests::InRandomOrder:
|
case RunTests::InRandomOrder:
|
||||||
seedRng( config );
|
seedRng( config );
|
||||||
RandomNumberGenerator::shuffle( sorted );
|
std::shuffle( sorted.begin(), sorted.end(), rng() );
|
||||||
break;
|
break;
|
||||||
case RunTests::InDeclarationOrder:
|
case RunTests::InDeclarationOrder:
|
||||||
// already in declaration order
|
// already in declaration order
|
||||||
|
Loading…
Reference in New Issue
Block a user