From 5a2a6074a4b209db93e211dcd160e43d4969a59e Mon Sep 17 00:00:00 2001 From: "Peter W. Draper" <p.w.draper@durham.ac.uk> Date: Mon, 6 Aug 2018 16:11:07 +0100 Subject: [PATCH] Add function to give a different seed for randoms --- src/clocks.c | 21 +++++++++++++++++++++ src/clocks.h | 1 + 2 files changed, 22 insertions(+) diff --git a/src/clocks.c b/src/clocks.c index cac0131aca..10519e6f53 100644 --- a/src/clocks.c +++ b/src/clocks.c @@ -30,6 +30,7 @@ /* Standard headers. */ #include <stdio.h> +#include <limits.h> #include <unistd.h> /* Local headers. */ @@ -280,3 +281,23 @@ double clocks_get_cputime_used(void) { times(&tmstic); return (double)(tmstic.tms_utime + tmstic.tms_cutime); } + +/** + * @brief Return an integer based on the current time. + * + * Normally this will be the remainder of the current number of nanoseconds + * so not very dissimilar in the most significant figures unless the time + * between calls is greater than INT_MAX nanoseconds. For faster calls use + * fewer figures, if that matters. + * + * @result an integer. + */ +int clocks_random_seed(void) { + struct timespec timespec; +#ifdef HAVE_CLOCK_GETTIME + clock_gettime(CLOCK_REALTIME, ×pec); + return (timespec.tv_nsec % INT_MAX); +#else + return (getticks() % INT_MAX); +#endif +} diff --git a/src/clocks.h b/src/clocks.h index f390158477..d33e5a342a 100644 --- a/src/clocks.h +++ b/src/clocks.h @@ -44,5 +44,6 @@ double clocks_diff_ticks(ticks tic, ticks toc); const char *clocks_get_timesincestart(void); double clocks_get_cputime_used(void); +int clocks_random_seed(void); #endif /* SWIFT_CLOCKS_H */ -- GitLab