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

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Mon Sep 25 12:57:36 PDT 2023


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

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

>From 850aedb355a4d4320f3d96203eb62004f469e2c7 Mon Sep 17 00:00:00 2001
From: Peter Klausler <pklausler at nvidia.com>
Date: Mon, 25 Sep 2023 12:55:10 -0700
Subject: [PATCH] [flang][runtime] Better non-repeatable RANDOM_INIT()

Use a higher-frequency clock base when initializing the
pseudo-random number generator to implement
  CALL  RANDOM_INIT(REPEATABLE=.FALSE.)
---
 flang/runtime/random.cpp | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

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



More information about the flang-commits mailing list