Fix broken random seed generation.
authorKevin Boyd <kevin44boyd@gmail.com>
Tue, 5 Jan 2021 13:28:26 +0000 (13:28 +0000)
committerMark Abraham <mark.j.abraham@gmail.com>
Tue, 5 Jan 2021 13:28:26 +0000 (13:28 +0000)
On Ryzen 3k CPUs with the RDRAND microcode bug, the
std::mt19937_64 alternate path yields the same seed
on multiple calls that fall within the same second. Changed
the "seed" of the random seed to rescramble each time the
makeRandomSeed() function is called.

Fixes #3859

src/gromacs/random/seed.cpp

index 79f67c9cfc2edbf572ba5daf4dcff6e4f20dbbd7..f046bd96bc9672426f5da9f8bec94b1faa5fedc2 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2015,2016,2018,2019,2020, by the GROMACS development team, led by
+ * Copyright (c) 2015,2016,2018,2019,2020,2021, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
@@ -37,7 +37,7 @@
 
 #include "seed.h"
 
-#include <time.h>
+#include <chrono>
 
 #include "gromacs/utility/basedefinitions.h"
 #include "gromacs/utility/fatalerror.h"
@@ -111,7 +111,8 @@ uint64_t makeRandomSeed()
     }
     else
     {
-        std::mt19937_64 prng(time(nullptr));
+        int64_t microsecondsSinceEpoch = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
+        std::mt19937_64 prng(microsecondsSinceEpoch);
         return makeRandomSeedInternal(prng);
     }
 }