I'm trying to do a programme in C, which should generate random numbers from 1 to 16, but when one number is picked it can't be picked twice, so all numbers must appear once and only once. But instead of this, some numbers are shown more than once. What's wrong?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int x[16];
int max = 16, min = 1;
srand(time(NULL));
for (int i = 0; i < 16; i++) {
x[i]= (rand() % (max-min+1)) + min;
for (int j = 0; j < i; j++) {
if (x[i] == x[j]) {
while (x[i] == x[j]) {
x[i] = (rand() % (max-min+1)) + min;
}
}
}
}
for (int i = 0; i < 16; i++) {
printf("\n%d", x[i]);
}
return 0;
}