I have written a simple random number generator in C. int l
is the lower bound and int u
is the upper bound.
It works just fine, however I have a question regarding seeding it. If I was to run this in a loop, time(NULL)
doesn't change the seed value quick enough to be able to prevent getting a consecutive series of random numbers that are exactly the same.
I'm wondering how anybody else might have approached this problem. All the examples I've found online use time(NULL)
as the seed value generator.
int generateRandom(int l, int u)
{
srand(time(NULL));
int r = rand() % ((u - l) + 1);
r = l + r;
return r;
}
If I was to run these lines of code right next to each other, both Rand1
and Rand2
would be exactly the same.
printf("Rand1 = %d\n", generateRandom(10, 46));
printf("Rand2 = %d\n", generateRandom(10, 46));
l
! Preferlb
,lower
,lowerbound
, ...