File tree Expand file tree Collapse file tree 2 files changed +12
-14
lines changed Expand file tree Collapse file tree 2 files changed +12
-14
lines changed Original file line number Diff line number Diff line change @@ -32,27 +32,19 @@ bool Random::operator==(const Random &o) const {
32
32
gen == o.gen ;
33
33
}
34
34
35
- bool static_gen_seeded = false ; // used only for seeding seed if 0/auto is passed for seed
36
- std::mt19937 static_gen;
35
+ std::random_device rd; // HW RNG, undeterministic, platform dependant. Use only for seeding rng if random seed wanted (seed=0)
37
36
38
37
Random::Random (UInt64 seed) {
39
38
if (seed == 0 ) {
40
- if ( !static_gen_seeded ) {
41
- #if NDEBUG
42
- unsigned int static_seed = (unsigned int )std::chrono::system_clock::now ().time_since_epoch ().count ();
43
- #else
44
- unsigned int static_seed = DEBUG_RANDOM_SEED;
45
- #endif
46
- static_gen.seed ( static_seed );
47
- static_gen_seeded = true ;
39
+ const unsigned int static_seed = rd ();
40
+ std::mt19937 static_gen (static_seed);
48
41
NTA_INFO << " Random seed: " << static_seed;
49
- }
42
+
50
43
seed_ = static_gen (); // generate random value from HW RNG
51
44
} else {
52
45
seed_ = seed;
53
46
}
54
- // if seed is zero at this point, there is a logic error.
55
- NTA_CHECK (seed_ != 0 );
47
+ NTA_CHECK (seed_ != 0 ) << " Random: if seed is zero at this point, there is a logic error" ;
56
48
gen.seed (static_cast <unsigned int >(seed_)); // seed the generator
57
49
steps_ = 0 ;
58
50
}
Original file line number Diff line number Diff line change @@ -37,8 +37,10 @@ using namespace std;
37
37
TEST (RandomTest, Seeding) {
38
38
{
39
39
Random r;
40
+ ASSERT_TRUE (r.getSeed () != 0 ) << " Should initialize with randomized seed" ;
41
+
40
42
auto x = r.getUInt32 ();
41
- ASSERT_TRUE (x != 0 );
43
+ ASSERT_NE (x, 0u );
42
44
}
43
45
44
46
// test getSeed
@@ -64,6 +66,10 @@ TEST(RandomTest, Seeding) {
64
66
ASSERT_EQ (r (), 419326371u );
65
67
}
66
68
69
+ for (int i=0 ; i< 10 ; i++) {
70
+ ASSERT_NE (Random (0 ).getSeed (), Random (0 ).getSeed ()) << " Randomly seeded generators should not be identical!" ;
71
+ }
72
+
67
73
}
68
74
69
75
You can’t perform that action at this time.
0 commit comments