From 300310ddc51a0022250a3b072f4c30c45470b857 Mon Sep 17 00:00:00 2001 From: Jean-Pierre PRUNARET Date: Thu, 19 Jan 2017 22:36:20 +0100 Subject: [PATCH] ajout code pour consruire un rand --- rand/main.c | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/rand/main.c b/rand/main.c index 7d59ee1..83ac801 100644 --- a/rand/main.c +++ b/rand/main.c @@ -1,3 +1,7 @@ +#define _POSIX_C_SOURCE 200809L + +#include +#include #include #include #include @@ -18,12 +22,45 @@ int rand_lim(int limit) { return retval; } +void print_current_time_with_ms (void) { + long ms; // Milliseconds + time_t s; // Seconds + struct timespec spec; + + clock_gettime(CLOCK_REALTIME, &spec); + + s = spec.tv_sec; + ms = round(spec.tv_nsec / 1.0e6); // Convert nanoseconds to milliseconds + + printf ( "Current time: %ld.%03ld seconds since the Epoch\n" + , (intmax_t)s + , ms + ); +} + +long int get_current_time_ms_part (void) { + long milliseconds; + time_t seconds; + struct timespec spec; + + clock_gettime(CLOCK_REALTIME, &spec); + + seconds = spec.tv_sec; + milliseconds = round(spec.tv_nsec / 1.0e6); // Convert nanoseconds to milliseconds + return spec.tv_nsec; +} + int main(int argc, char **argv) { - srand(time(NULL)); + /** + * time precision is 1 second. 2+ calls in same second return same value + */ + //srand(time(NULL)); + srand(get_current_time_ms_part()); int random_value = rand_lim(1); - printf("value: %d\n", random_value); + //printf("value: %d\n", random_value); + printf("%d", random_value); //return rand_lim(1); return random_value;