#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, const char *argv[]) {
srand(clock());
int num = rand() % 6 + 1;
printf("%i", num);
return 0;
}
I get this warning in “srand(clock());” line.
Warning: Implicit conversion loses integer precision: ‘clock_t’ (aka ‘unsigned long’) to ‘unsigned int’
How do I fix it? Thanks!
Don’t use srand(clock()) use srand((unsigned)time(NULL)) instead.
Better seeds:
Use time(NULL) to get the time of day and cast the result to seed
srand().
time(NULL) returns the number of seconds elapsed since midnight
January 1st, 1970.
Use rdtsc() to get the CPU timestamp and cast the result to seed
srand().
rdtsc() is unlikely to return duplicate values as it returns the
number of instructions
executed by the processor since startup.
You should also read this article at US-CERT Secure Coding on how properly seed.