Browse Source

Enhanced seeding for random number generation

Paul-Louis Ageneau 2 years ago
parent
commit
2b75ba18bb
1 changed files with 13 additions and 5 deletions
  1. 13 5
      src/impl/utils.cpp

+ 13 - 5
src/impl/utils.cpp

@@ -20,11 +20,13 @@
 
 
 #include "impl/internals.hpp"
 #include "impl/internals.hpp"
 
 
+#include <algorithm>
 #include <cctype>
 #include <cctype>
 #include <chrono>
 #include <chrono>
 #include <functional>
 #include <functional>
 #include <iterator>
 #include <iterator>
 #include <sstream>
 #include <sstream>
+#include <thread>
 
 
 namespace rtc::impl::utils {
 namespace rtc::impl::utils {
 
 
@@ -118,14 +120,20 @@ std::seed_seq random_seed() {
 	try {
 	try {
 		// On some systems an exception might be thrown if the random_device can't be initialized
 		// On some systems an exception might be thrown if the random_device can't be initialized
 		std::random_device device;
 		std::random_device device;
-		seed.push_back(device());
-	} catch (const std::exception &) {
+		// 128 bits should be more than enough
+		std::generate_n(std::back_inserter(seed), 4, std::ref(device));
+	} catch (...) {
 		// Ignore
 		// Ignore
 	}
 	}
 
 
-	// Seed with current time
-	using std::chrono::system_clock;
-	seed.push_back(static_cast<unsigned int>(system_clock::now().time_since_epoch().count()));
+	// Seed with high-resolution clock
+	using std::chrono::high_resolution_clock;
+	seed.push_back(
+	    static_cast<unsigned int>(high_resolution_clock::now().time_since_epoch().count()));
+
+	// Seed with thread id
+	seed.push_back(
+	    static_cast<unsigned int>(std::hash<std::thread::id>{}(std::this_thread::get_id())));
 
 
 	return std::seed_seq(seed.begin(), seed.end());
 	return std::seed_seq(seed.begin(), seed.end());
 }
 }