|
|
|
#define _POSIX_C_SOURCE 200809L
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a random number between 0 and limit inclusive.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
int rand_lim(int limit) {
|
|
|
|
|
|
|
|
int divisor = RAND_MAX/(limit+1);
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
do {
|
|
|
|
retval = rand() / divisor;
|
|
|
|
} while (retval > 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) {
|
|
|
|
/**
|
|
|
|
* 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("%d", random_value);
|
|
|
|
|
|
|
|
//return rand_lim(1);
|
|
|
|
return random_value;
|
|
|
|
};
|
|
|
|
|