agree with @anon57585045, since you have all the numbers between 0 and SIZE-1 in your array of size SIZE, the simplest way that will lead to guaranteed timing is to fill the array in sequential order and then shuffle.
There is a well know shuffle algorithm called the Fisher–Yates shuffle
try this:
const int SIZE = 10;
int array[SIZE] ;
// Fisher–Yates shuffle
template<typename T>
void shuffleArray(T* a, const size_t size) {
for (size_t i = size - 1; i > 0; --i) {
size_t r = random(0, i + 1); // generate rand numbers from 0 to i
T tmpSwap = a[i];
a[i] = a[r];
a[r] = tmpSwap;
}
}
void printArray(const int* a, const size_t size) {
for (size_t i = 0; i < size; i++) {
Serial.print(a[i]); Serial.write(' ');
}
Serial.println();
}
void setup() {
Serial.begin(115200); Serial.println();
// randomize
randomSeed(analogRead(A0));
// fill the array in sequential order
for (int i = 0; i < SIZE; i++) array[i] = i;
printArray(array, SIZE);
// shuffle the array
shuffleArray(array, SIZE);
printArray(array, SIZE);
}
void loop() {}
or if template is intimidating
const int SIZE = 10;
int array[SIZE] ;
// Fisher–Yates shuffle
void shuffleArray(int* a, const size_t size) {
for (size_t i = size - 1; i > 0; --i) {
size_t r = random(0, i + 1); // generate rand numbers from 0 to i
int tmpSwap = a[i];
a[i] = a[r];
a[r] = tmpSwap;
}
}
void printArray(const int* a, const size_t size) {
for (size_t i = 0; i < size; i++) {
Serial.print(a[i]); Serial.write(' ');
}
Serial.println();
}
void setup() {
Serial.begin(115200); Serial.println();
// randomize
randomSeed(analogRead(A0));
// fill the array in sequential order
for (int i = 0; i < SIZE; i++) array[i] = i;
printArray(array, SIZE);
// shuffle the array
shuffleArray(array, SIZE);
printArray(array, SIZE);
}
void loop() {}