Skip to content

Commit c25abe9

Browse files
committed
lfortran_intrinsics.c: Use nanoseconds to seed random
This ensures srand() gets different (far-away) values for two subsequent executions of the compiler.
1 parent c610e31 commit c25abe9

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/libasr/runtime/lfortran_intrinsics.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,18 @@ LFORTRAN_API void _lfortran_init_random_seed(unsigned seed)
115115

116116
LFORTRAN_API void _lfortran_init_random_clock()
117117
{
118-
srand((unsigned int)clock());
118+
unsigned int count;
119+
#if defined(_MSC_VER)
120+
count = (unsigned int)clock();
121+
#else
122+
struct timespec ts;
123+
if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
124+
count = (unsigned int)(ts.tv_nsec);
125+
} else {
126+
count = (unsigned int)clock();
127+
}
128+
#endif
129+
srand(count);
119130
}
120131

121132
LFORTRAN_API double _lfortran_random()

0 commit comments

Comments
 (0)