Browse Source

Merge pull request #759 from paullouisageneau/utils-random-better-seed

Enhance seeding for random number generation
Paul-Louis Ageneau 2 years ago
parent
commit
6701a03799
3 changed files with 17 additions and 9 deletions
  1. 2 2
      examples/client-benchmark/main.cpp
  2. 2 2
      examples/client/main.cpp
  3. 13 5
      src/impl/utils.cpp

+ 2 - 2
examples/client-benchmark/main.cpp

@@ -479,9 +479,9 @@ shared_ptr<rtc::PeerConnection> createPeerConnection(const rtc::Configuration &c
 
 
 // Helper function to generate a random ID
 // Helper function to generate a random ID
 std::string randomId(size_t length) {
 std::string randomId(size_t length) {
-	using std::chrono::system_clock;
+	using std::chrono::high_resolution_clock;
 	static thread_local std::mt19937 rng(
 	static thread_local std::mt19937 rng(
-	    static_cast<unsigned int>(system_clock::now().time_since_epoch().count()));
+	    static_cast<unsigned int>(high_resolution_clock::now().time_since_epoch().count()));
 	static const std::string characters(
 	static const std::string characters(
 	    "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
 	    "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
 	std::string id(length, '0');
 	std::string id(length, '0');

+ 2 - 2
examples/client/main.cpp

@@ -264,9 +264,9 @@ shared_ptr<rtc::PeerConnection> createPeerConnection(const rtc::Configuration &c
 
 
 // Helper function to generate a random ID
 // Helper function to generate a random ID
 std::string randomId(size_t length) {
 std::string randomId(size_t length) {
-	using std::chrono::system_clock;
+	using std::chrono::high_resolution_clock;
 	static thread_local std::mt19937 rng(
 	static thread_local std::mt19937 rng(
-	    static_cast<unsigned int>(system_clock::now().time_since_epoch().count()));
+	    static_cast<unsigned int>(high_resolution_clock::now().time_since_epoch().count()));
 	static const std::string characters(
 	static const std::string characters(
 	    "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
 	    "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
 	std::string id(length, '0');
 	std::string id(length, '0');

+ 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());
 }
 }