Hi all. I'm trying to add random non-repeat numbers to an array but I can't seem to get it to work the first time through the loop. It takes a few times through to complete the list. Can someone give me some advice? Thanks
#define SIZE 5
int array[SIZE] ;
int count = 0;
int intRan = 0;
bool boolExists = false;
bool boolRunOnce = false;
unsigned long tim = 0;
void setup() {
Serial.begin(9600);
for (int i = 0; i < SIZE; i++) {
array[i] = -1;
}
}
void loop() {
if (not boolRunOnce) {
for (int i = 0; i < SIZE; i++) {
intRan = random(0, SIZE);
for (int x = 0; x < SIZE; x++) {
int intRead = array[x];
if (intRead == intRan) {
boolExists = true;
}
}
if (not boolExists) {
array[i] = intRan;
}
boolExists = false;
}
delay(500);
for (int i = 0; i < SIZE; i++) {
Serial.println(array[i]);
}
}
Serial.println("");
Serial.println("");
}
And what happened to the forum lol? I'm not fond of the new template but maybe I just need to get used to it.
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.