From f9f5775ccc1489ec5416d4d327a3f4dcd8ea5cc4 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Sat, 14 Mar 2020 15:21:53 -0700 Subject: [PATCH] Switch to C++ random number generator for history vacuum interval --- src/history.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/history.cpp b/src/history.cpp index 9ef4e4887..d783035d0 100644 --- a/src/history.cpp +++ b/src/history.cpp @@ -14,17 +14,18 @@ // We need the sys/file.h for the flock() declaration on Linux but not OS X. #include // IWYU pragma: keep #include -#include #include #include #include #include +#include #include #include #include #include #include +#include #include #include @@ -396,10 +397,12 @@ void history_impl_t::save_unless_disabled() { // the counter. const int kVacuumFrequency = 25; if (countdown_to_vacuum < 0) { - unsigned int seed = static_cast(time(nullptr)); // Generate a number in the range [0, kVacuumFrequency). - srand(seed); - countdown_to_vacuum = rand() / (RAND_MAX / kVacuumFrequency + 1); + std::uniform_int_distribution dist{0, kVacuumFrequency - 1}; + unsigned seed = + static_cast(std::chrono::system_clock::now().time_since_epoch().count()); + std::minstd_rand gen{seed}; + countdown_to_vacuum = dist(gen); } // Determine if we're going to vacuum.