/*
  Avoid repeating a random number
  https://forum.arduino.cc/t/avoid-repeating-a-random-number-within-a-set-time/1033379/

  items needs to be a lot larger than the size of the used buffer!
*/
const uint8_t items = 42;  // items on list
const uint8_t size = 10;   // size of ring buffer for already used items
uint8_t used[size];        // stores already used colors
byte index;                // ring buffer index

constexpr uint8_t btn {2};

void initUsed()
{
  for (byte i = 0; i < size; i++)
  {
    used[i] = random(0, items);    // scramble the content of the array (to allow 0 in the first n trys also)
  }
}

bool isUsed(byte actual)
{
  bool result = false;
  for (byte i = 0; i < size; i++)
  {
    if (actual == used[i]) return true;
  }
  return false;
}

int getRandomNumber()
{
  bool found = false;
  int rnd;
  while (found == false)
  {
    rnd = random(0, items);  
    if (!isUsed(rnd))
    {
      used[index] = rnd;
      index++;
      if (index >= size) index = 0;
      found = true;
    }
  }
  return rnd;
}

void action()
{
  byte actual = getRandomNumber();
    Serial.print("index: "); Serial.print(index);
    Serial.print("\tactual: "); Serial.print(actual);
    Serial.println();
    delay(100);
}


void setup() {
  Serial.begin(115200);
  Serial.println(F("Random with some uniqueness for the last minutes"));
  randomSeed(analogRead(A4));
  pinMode(btn, INPUT_PULLUP);
  initUsed(); 
}

void loop() {
  if (digitalRead(btn) == LOW) action();
  delay(50); // Allows a comfortable (brief) press

}