Random number generator in C with srand() and time(). Srand() is a helper function for the rand() function, generating a seed for the start of a sequence of (pseudo)random numbers. Because the time returned by the time() function changes once a second, when calling this code more often, there will be no randomness. To be – use nanoseconds through timespec_get ().
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
int n, r;
printf("Rng at your service:\n\n");
srand((unsigned)time(NULL));
for (n = 0; n < 10; n++)
{
r = rand();
printf("%d\t", r % 100);
}
return 0;
}