[flang-commits] [flang] [flang][runtime] Better non-repeatable RANDOM_INIT() (PR #67363)

via flang-commits flang-commits at lists.llvm.org
Mon Sep 25 12:59:11 PDT 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-runtime

<details>
<summary>Changes</summary>

Use a higher-frequency clock base when initializing the pseudo-random number generator to implement
  CALL  RANDOM_INIT(REPEATABLE=.FALSE.)

---
Full diff: https://github.com/llvm/llvm-project/pull/67363.diff


1 Files Affected:

- (modified) flang/runtime/random.cpp (+4-2) 


``````````diff
diff --git a/flang/runtime/random.cpp b/flang/runtime/random.cpp
index b7175d6b63c35f8..93113657bdecbca 100644
--- a/flang/runtime/random.cpp
+++ b/flang/runtime/random.cpp
@@ -20,10 +20,10 @@
 #include <algorithm>
 #include <cmath>
 #include <cstdint>
-#include <ctime>
 #include <limits>
 #include <memory>
 #include <random>
+#include <time.h>
 
 namespace Fortran::runtime {
 
@@ -100,7 +100,9 @@ void RTNAME(RandomInit)(bool repeatable, bool /*image_distinct*/) {
     if (repeatable) {
       generator.seed(0);
     } else {
-      generator.seed(std::time(nullptr));
+      timespec ts;
+      clock_gettime(CLOCK_REALTIME, &ts);
+      generator.seed(ts.tv_sec & ts.tv_nsec);
     }
   }
 }

``````````

</details>


https://github.com/llvm/llvm-project/pull/67363


More information about the flang-commits mailing list