I was asked to not use rand()
because they were not "thread safe" and to also use a different seed value each time. I found examples on GitHub using a seed value like this:
unsigned int seed = time(NULL);
That only has a precision by seconds. Since the program runs in under 1 second, I end up getting the same random number every instance.
How would I fix this algorithm so that it only uses rand_r()
or any other "thread safe" methods to generate 10 random numbers?
int main()
{
for(int i = 0; i < 10; i++){
int random;
unsigned int seed = time(NULL);
random = 1 + (rand_r(&seed)% 10);
printf("%d\n",random);
}
return 0;
}
Aucun commentaire:
Enregistrer un commentaire