int array[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
size_t arraySize = sizeof array / sizeof * array;
void printArray() {
for (size_t i = 0; i < arraySize; ++i) {
Serial.print(array[i]);
Serial.write(' ');
}
Serial.println();
}
void fisherYatesShuffle() {
// Start from the last element and swap it with a randomly selected element before it
for (int i = arraySize - 1; i > 0; --i) {
// Generate a random index between 0 and i (inclusive)
int randomIndex = random(0, i + 1);
// Swap array[i] with the randomly selected element
int temp = array[i];
array[i] = array[randomIndex];
array[randomIndex] = temp;
}
}
void setup() {
Serial.begin(115200);
// create a bit of randomness
randomSeed(analogRead(A0));
Serial.println(F("Before shuffling"));
printArray();
// shuffle the array
fisherYatesShuffle();
Serial.println(F("After shuffling"));
printArray();
}
void loop() {}