diff --git a/src/clocks.c b/src/clocks.c index cac0131acade08e41ee7ed4a22fabde49e197060..10519e6f5341b4fa31b8bc8e284149cee3b423c6 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 f3901584774c7586d6a68b4415d6b443cb53c466..d33e5a342a9b7024ee918a035547e8351b3dc726 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 */