I want to generate a random number r in the loop, but I always get the same number N times. How can I achieve the desired effect?
And because it's part of the algorithm, it needs to be fast
while(r==i)
Because I need a random number that's different than i
for (int i = 0; i < N; i++)
{
srand(time(0));
do {
r = rand() % N;
} while (r == i);
}
Even if the srand()
function is outside the loop,after run N times,r
is still the same
srand(time(0));
for (int i = 0; i < N; i++)
{
do {
r = rand() % N;
} while (r == i);
}