If you want to fill a 20 element array with random numbers from 0 to 19 without repetition, then fill them with 0 to 19 normally, then randomly swap indices.
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#define N 20
int main(void) {
srand(time(NULL));
int arr[N];
for (size_t i = 0; i < N; i++) {
arr[i] = i;
}
for (size_t i = 0; i < N; i++) {
int r1 = rand() % N;
int r2 = rand() % N;
int temp = arr[r1];
arr[r1] = arr[r2];
arr[r2] = temp;
}
for (size_t i = 0; i < N; i++) {
printf("%d\n", arr[i]);
}
}
https://godbolt.org/z/avKGozfoh
As pointed out in comments, the above represents a naive algorithm. Using a hopefully faithful implementation of the Knuth/Fisher-Yates algorithm, we get an even distribution of results.
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#define N 20
int main(void) {
srand(time(NULL));
int arr[N];
for (size_t i = 0; i < N; i++) {
arr[i] = i;
}
for (size_t i = 0; i < N; i++) {
size_t j = rand() % (N - i) + i;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
for (size_t i = 0; i < N; i++) {
printf("%d\n", arr[i]);
}
}
https://godbolt.org/z/qsfYa8r54