From 38d911f50261cfc8aa4e63781bf16b900b78e83c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Tr=C3=A1vn=C3=AD=C4=8Dek?= <jan.travnicek@fit.cvut.cz>
Date: Mon, 27 Dec 2021 20:11:11 +0100
Subject: [PATCH] graph: generate graphs in a reproducible way

---
 alib2graph_algo/src/generate/RandomGraphFactory.hpp  | 12 +++++-------
 alib2graph_algo/src/generate/RandomGridFactory.hpp   |  7 +++----
 .../test-src/shortest_path/ShortestPathTest.cpp      |  4 ++++
 3 files changed, 12 insertions(+), 11 deletions(-)

diff --git a/alib2graph_algo/src/generate/RandomGraphFactory.hpp b/alib2graph_algo/src/generate/RandomGraphFactory.hpp
index c98981e0c8..9c3cc6d54b 100644
--- a/alib2graph_algo/src/generate/RandomGraphFactory.hpp
+++ b/alib2graph_algo/src/generate/RandomGraphFactory.hpp
@@ -5,7 +5,7 @@
 #include <alib/pair>
 #include <alib/vector>
 #include <alib/tuple>
-#include <random>
+#include <ext/random>
 
 namespace graph {
 
@@ -40,9 +40,8 @@ RandomGraphFactory::randomGraph(unsigned long max_nodes,
                                 unsigned long branching_factor) {
   using node_type = typename TGraph::node_type;
 
-  // Seed with a real random value, if available
-  std::random_device r;
-  std::default_random_engine e1(r());
+  // Seed with a random value, if available
+  std::default_random_engine e1(ext::random_devices::semirandom());
 
   // Create distribution
   std::uniform_int_distribution<unsigned long> number_of_nodes(1, max_nodes); // Generate number of nodes
@@ -97,9 +96,8 @@ RandomGraphFactory::randomWeightedGraph(
   using node_type = typename TGraph::node_type;
   using weight_type = typename TGraph::edge_type::weight_type;
 
-  // Seed with a real random value, if available
-  std::random_device r;
-  std::default_random_engine e1(r());
+  // Seed with a random value, if available
+  std::default_random_engine e1(ext::random_devices::semirandom());
 
   // Create distribution
   std::uniform_int_distribution<unsigned long> number_of_nodes(1, max_nodes); // Generate number of nodes
diff --git a/alib2graph_algo/src/generate/RandomGridFactory.hpp b/alib2graph_algo/src/generate/RandomGridFactory.hpp
index afc2f599f3..a2c68d4ae6 100644
--- a/alib2graph_algo/src/generate/RandomGridFactory.hpp
+++ b/alib2graph_algo/src/generate/RandomGridFactory.hpp
@@ -5,7 +5,7 @@
 #include <alib/pair>
 #include <alib/tuple>
 #include <alib/vector>
-#include <random>
+#include <ext/random>
 
 namespace graph {
 
@@ -34,9 +34,8 @@ RandomGridFactory::randomGrid(unsigned long height,
   using coordinate_type = typename TGrid::coordinate_type;
   using node_type = typename TGrid::node_type;
 
-  // Seed with a real random value, if available
-  std::random_device r;
-  std::default_random_engine e1(r());
+  // Seed with a random value, if available
+  std::default_random_engine e1(ext::random_devices::semirandom());
 
   // Create distribution
   std::uniform_int_distribution<unsigned long> number_of_obstacles(0, max_obstacles); // Generate number of nodes
diff --git a/alib2graph_algo/test-src/shortest_path/ShortestPathTest.cpp b/alib2graph_algo/test-src/shortest_path/ShortestPathTest.cpp
index c07f6be76e..5b542f20e7 100644
--- a/alib2graph_algo/test-src/shortest_path/ShortestPathTest.cpp
+++ b/alib2graph_algo/test-src/shortest_path/ShortestPathTest.cpp
@@ -343,6 +343,10 @@ TEST_CASE ( "Shortest Path", "[unit][graph][shortest_path]" ) {
 	// ---------------------------------------------------------------------------------------------------------------------
 
 	SECTION ( "All Grid Random" ) {
+		unsigned seed = ext::random_devices::random ( );
+		ext::random_devices::semirandom.seed ( seed );
+		INFO ( "seed: " << seed );
+
 		auto graph_tuple = generate::RandomGridFactory::randomGrid<grid::WeightedSquareGrid8<long, edge::WeightedEdge<ext::pair<long, long>>>>(50, 50, 100);
 		auto graph = std::get<0>(graph_tuple);
 		auto start = std::get<1>(graph_tuple);
-- 
GitLab