Search for number generated by random number generator

I have built a circuit consisting of two LEDs which are to do the following: I am generating a random number. If this random number equals a certain value, one LED is suppossed to blink. Within 5 cycles I am trying to find this number. In each cycle the other LED is blinking. Something is clearly wrong with this code:

/* We are looking for a specific value to be produced by the random number generator; when we find it, we light the foundit-LED, when we cycle through the loop, we light the other LED*/

#define IOPIN1 10
#define IOPIN2 11
#define PAUSE 1000
#define LOOKINGFOR 443
#define MAXCYCLES 5


int foundIt = IOPIN1;
int cycle = IOPIN2;
int randomnumber=0;
int loopcounter;

// The setup routine runs once when you press reset or apply power
void setup(){
  Serial.begin(115200);
  pinMode(foundIt, OUTPUT);
  pinMode(cycle, OUTPUT);
  randomSeed(analogRead(0));  // if analog input pin 0 is unconnected, random analog is read
                              // noise will cause the call to randomSeed() to generate
                              // different seed numbers each time the sketch runs.
                              // randomSeed() will then shuffle the random function.
}

// The loop routine runs over and over again
void loop() {
  loopcounter = 0;
  randomnumber = random(0,5000);
  while((randomnumber != LOOKINGFOR) && (loopcounter < MAXCYCLES)) {
    Serial.println(randomnumber);
    digitalWrite(cycle, HIGH);
    delay(PAUSE);
    digitalWrite(cycle, LOW);
    delay(PAUSE);
    randomnumber = random(0,5000);
    loopcounter++;
    if(randomnumber == LOOKINGFOR) {
      digitalWrite(foundIt, HIGH);
      delay(PAUSE);
      digitalWrite(foundIt, LOW);
      delay(PAUSE);
      Serial.print("Found it! Loopcycles needed: "); Serial.print(loopcounter);
      }
    if(loopcounter == MAXCYCLES){
        Serial.println("Couldn't find it within 5 Loopcycles!");
        }
   }
}

Because the output that I receive is the follwing:

2379
4209
3528
3472
693
Couldn’t find it within 5 Loopcycles!
4322

I expected only 5 randomly generated numbers before the statement “Couldn’t find it within 5 Loopcycles!”. Where does this last number come from?!

Interestingly this does not happen when initializing loopcounter = 0 globally. However, in that case this program doesn’t run for ever but only once, because the loopcounter is then greater than MAXCYCLES.

Please help me figure this out!

Maybe something is wrong with the idea. Why should your search number turn up within 5 cycles?

As for the number 4322, the loop() function loops, as its name implies.

I mean, I could let the loop() function loop for ever. Maybe it is the better idea. But still, I don’t understand why the code that I have written doesn’t work the way I intended to.

Luk_11:
I mean, I could let the loop() function loop for ever. Maybe it is the better idea. But still, I don’t understand why the code that I have written doesn’t work the way I intended to.

perhaps like this:

/* We are looking for a specific value to be produced by the random number generator; when we find it, we light the foundit-LED, when we cycle through the loop, we light the other LED*/

#define IOPIN1 10
#define IOPIN2 11
#define PAUSE 1000
#define LOOKINGFOR 443
#define MAXCYCLES 5


int foundIt = IOPIN1;
int cycle = IOPIN2;
int randomnumber = 0;
int loopcounter;

// The setup routine runs once when you press reset or apply power
void setup() {
  Serial.begin(115200);
  pinMode(foundIt, OUTPUT);
  pinMode(cycle, OUTPUT);
  randomSeed(analogRead(0));  // if analog input pin 0 is unconnected, random analog is read
  // noise will cause the call to randomSeed() to generate
  // different seed numbers each time the sketch runs.
  // randomSeed() will then shuffle the random function.
}

void loop() 
{
  loopcounter = 0;
  bool found = false;
  do{
    randomnumber = random(0, 5000);
    loopcounter++;
    if (randomnumber == LOOKINGFOR) 
    {
      digitalWrite(foundIt, HIGH);
      delay(PAUSE);
      digitalWrite(foundIt, LOW);
      delay(PAUSE);
      Serial.print("Found it! Loopcycles needed: "); Serial.println(loopcounter);
      found = true;
    }
  }while((randomnumber != LOOKINGFOR) && (loopcounter < MAXCYCLES));
  
  if(!found)
  {
    //Serial.println(".");
  }
}

I expected only 5 randomly generated numbers before the statement "Couldn't find it within 5 Loopcycles!". Where does this last number come from?!

Are you sure that the last number was the last one?

Jacques