[resolved] reduce number of elements in array ?

Hello,

this certainly simple answer but i'm a bit lost with pointers and C

I have an array with 12 elements and want randomly choose one...
BUT i want only choose one time each element... until all the elements has been choosen.

In an other langage i'll use a copy of my array and splice() each new element choosen from my array's copy...
But in C... i don't know how to do that.

any idea ?

thanks. :*

You could maintain a shadow boolean array to see if the number has been chosen before, or simply negate the value in the array, if you're only expecting positive numbers.

Thanks very much...

I've tried your shadow array purpose and give the code.

thanks again

int array[] = {0,2,4,6,8,10};
const int arrayLength = sizeof(array)/sizeof(array[0]);
boolean shadowArray[arrayLength];
int t;
int compteur;

void setup() {
  Serial.begin(9600);
  // fill the shadow array
  for (int i=0; i<arrayLength; i++) {
    shadowArray[i] = false;
  }
}

void loop() {
  if (compteur == arrayLength) {
    for (int i=0; i<arrayLength; i++) {
      shadowArray[i] = false;
    }
    compteur = 0;
    Serial.println("reinit shadowArray\n");
  }
  
  do {t= int(random(arrayLength));}
  while (shadowArray[t]);
  
  Serial.println(array[t]);
  shadowArray[t] = true;
  compteur++;
 
  delay(2000);
}