You need to add a separate loop that goes from 0
, inclusive, to i
, exclusive, checking the candidate number against the numbers that have been added to the array before. If the check finds a duplicate, do not increment i
, and try generating a random number again:
int i = 0;
while (i != 6) {
int candidate = ((rand() % 49) + 1);
int ok = 1; // ok will become 0 if a duplicate is found
for (int j = 0 ; ok && j != i ; j++) {
ok &= (candidate != Array[j]);
}
if (ok) {
Array[i++] = candidate;
printf("%d ", candidate);
}
}