The system call function gettimeofday() is a really helpful one.
Indeed, we can retrieve milliseconds of the time.
But in this tutorial, we are going to see how to generate random numbers every millisecond.
Here the code:
#include <stdlib.h> #include <stdio.h> #include <dlfcn.h> #include <unistd.h> #include <sys/time.h> int my_random() { double seed; struct timeval tv; gettimeofday(&tv, NULL); seed = (tv.tv_sec) * 1000 + (tv.tv_usec) / 1000; srand(seed); usleep(10000); return (rand() % 255); } int main () { int i = 0; while (i < 10) { printf("R = %d\n", my_random()); ++i; } return (0); }
© Badprog - I want to change the world. And I will.