I am writing a function that is supposed to populate an array with random numbers from 0 to n (where n is passed argument to the function), but all numbers in array should be unique. I basically need to shuffle the array of numbers from 0 to n
I found this answer here: Unique random numbers in an integer array in the C programming language
And used the "Knuth Algorithm" suggested by user:
void generate_random_array(int count)
{
int in, im;
im = 0;
srand(time(NULL));
for (in = 0; in < count && im < count; ++in) {
int rn = count - in;
int rm = count - im;
if (rand() % rn < rm) random_array[im++] = in;
}
}
However this function does not generate random numbers for me at all, its simply creates an array of numbers from 0 to count. How can I generate the actual random sequence of unique numbers.