ajout code pour consruire un rand

master
Jean-Pierre PRUNARET 8 years ago
parent 49e98fcb2c
commit 300310ddc5

@ -1,3 +1,7 @@
#define _POSIX_C_SOURCE 200809L
#include <inttypes.h>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
@ -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;

Loading…
Cancel
Save