From eb6f3f86e5c64a14c06e24894c0ff64b4b99a462 Mon Sep 17 00:00:00 2001 From: Eric Liang Date: Thu, 12 Dec 2019 21:41:19 -0800 Subject: [PATCH] Seed using multiple samples (#6471) --- src/ray/util/util.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/ray/util/util.h b/src/ray/util/util.h index 4e4f71ccd..7f7e864b4 100644 --- a/src/ray/util/util.h +++ b/src/ray/util/util.h @@ -8,6 +8,7 @@ #include #include #include +#include #include #include "ray/common/status.h" @@ -95,6 +96,13 @@ void FillRandom(T *data) { RAY_CHECK(data != nullptr); auto randomly_seeded_mersenne_twister = []() { auto seed = std::chrono::high_resolution_clock::now().time_since_epoch().count(); + // To increase the entropy, mix in a number of time samples instead of a single one. + // This avoids the possibility of duplicate seeds for many workers that start in + // close succession. + for (int i = 0; i < 128; i++) { + std::this_thread::sleep_for(std::chrono::microseconds(10)); + seed += std::chrono::high_resolution_clock::now().time_since_epoch().count(); + } std::mt19937 seeded_engine(seed); return seeded_engine; };