[flang-commits] [flang] [flang][runtime] Correct RANDOM_INIT seed generation (PR #106250)

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Tue Aug 27 10:50:01 PDT 2024


https://github.com/klausler updated https://github.com/llvm/llvm-project/pull/106250

>From c995977a2a355aa604a1939093fe50bf2e072865 Mon Sep 17 00:00:00 2001
From: Peter Klausler <pklausler at nvidia.com>
Date: Tue, 27 Aug 2024 10:07:00 -0700
Subject: [PATCH] [flang][runtime] Correct RANDOM_INIT seed generation

The initial seed was generated from a bitwise AND ("&") of
two clock-generated values, instead of an XOR (best) or a
truncated integer multiplication.  Maybe I mistyped a shift-7
instead of a shift-6 or shift-8 when I wrote that line, but it
was most likely just stupidity.

Fixes https://github.com/llvm/llvm-project/issues/106221.
---
 flang/runtime/random.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/flang/runtime/random.cpp b/flang/runtime/random.cpp
index e0a421fd283962..69de9b8c96fb5d 100644
--- a/flang/runtime/random.cpp
+++ b/flang/runtime/random.cpp
@@ -42,7 +42,7 @@ void RTNAME(RandomInit)(bool repeatable, bool /*image_distinct*/) {
 #ifdef CLOCK_REALTIME
       timespec ts;
       clock_gettime(CLOCK_REALTIME, &ts);
-      generator.seed(ts.tv_sec & ts.tv_nsec);
+      generator.seed(ts.tv_sec ^ ts.tv_nsec);
 #else
       generator.seed(time(nullptr));
 #endif



More information about the flang-commits mailing list