From e235fc704dba8726d69f3f5964aa986952488231 Mon Sep 17 00:00:00 2001 From: Roland Coeurjoly Date: Thu, 27 Jun 2024 02:09:19 +0200 Subject: [PATCH] Create std::mt19937 only once --- tests/functional/single_cells/vcd_harness.cc | 21 ++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/tests/functional/single_cells/vcd_harness.cc b/tests/functional/single_cells/vcd_harness.cc index 18c9ed5df..9b3777c37 100644 --- a/tests/functional/single_cells/vcd_harness.cc +++ b/tests/functional/single_cells/vcd_harness.cc @@ -34,9 +34,7 @@ struct Dump { }; template -Signal random_signal() { - std::random_device rd; // Random device for seeding - std::mt19937 gen(rd()); // Mersenne Twister engine +Signal random_signal(std::mt19937 &gen) { std::uniform_int_distribution dist; std::array words; for(auto &w : words) @@ -52,9 +50,12 @@ struct Reset { }; struct Randomize { + std::mt19937 &gen; + Randomize(std::mt19937 &gen) : gen(gen) {} + template void operator()(const char *, Signal &signal) { - signal = random_signal(); + signal = random_signal(gen); } }; @@ -89,7 +90,7 @@ int main(int argc, char **argv) vcd_file << "#0\n"; // Set all signals to false inputs.visit(Reset()); - + gold::eval(inputs, outputs, state, next_state); { Dump d(vcd_file); @@ -97,11 +98,15 @@ int main(int argc, char **argv) outputs.visit(d); state.visit(d); } - + + // Initialize random number generator once + std::random_device rd; + std::mt19937 gen(rd()); + for (int step = 0; step < steps; ++step) { // Functional backend cxx vcd_file << "#" << (step + 1) << "\n"; - inputs.visit(Randomize()); + inputs.visit(Randomize(gen)); gold::eval(inputs, outputs, state, next_state); { @@ -110,7 +115,7 @@ int main(int argc, char **argv) outputs.visit(d); state.visit(d); } - + state = next_state; }