2
Let’s assume I have an empty array of size 8.
int A[8];
And then I intend to fill it with random values whose I would have a function similar to the rand();
However, I call on rand()
for each element of the array, so all elements have the same value.
So far I have that function:
int randInt(int intMax)
{
srand(time(NULL));
return rand() % (intMax+1);
}
In case it would return the same value if I called this function two or more times, because it is related to time. My question would be how can I implement a function that returns a random value even calling it twice or more?
srand
must be executed only once in the lifetime of the application, except when you want to generate the same sequence of random numbers or specific sequences, which is not your case. I didn’t see any answer say this explicitly. So normally,srand
is called right at the startup of the application, within themain
.– Vinícius Gobbo A. de Oliveira