Random blinking LEDs without the same LED blinking twice in a row.

Hello, I have been trying to let 4 LEDs blink randomly (with a delay I can change with a potentiometer). This works just fine, but I have a question. In this code, there is a change that the "random number generator" generates the same number twice or even three times after one another. If this is the case, a LED blinks twice or three times as long which kills the effect. Is it possible to code that it is impossible for the "random number generator" to generate the same number twice without a different number in between?
If it is unclear: here is an example.
Red blinks once, Green once, but then Blue is chosen twice in a row so it blinks twice as long.
This kills the random blinking effect.

int ranNum;
int pot = A0;
void setup() {
Serial.begin(9600);
randomSeed(analogRead(0));

pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
}
void loop() {
 int snelheid = analogRead(pot);
 snelheid = map(snelheid, 0, 1024, 100, 1000);
 ranNum=random(2,6);
  
 Serial.println(snelheid);

 digitalWrite(ranNum, HIGH);
 delay(snelheid);
 digitalWrite(ranNum, LOW); 
  
}

Sure. Just save the random number last used then if the next random number is the same, ignore it.

ranNum=random(2,6);
while (ranNum <> prevRndNum) ranNum=random(2,6);
prevRndNum = ranNum;
 do {
     ranNum = random(2, 6);
  }
  while (ranNum  == ranNum1);
  ranNum1 = ranNum;

Thank you very much. This wil help!

Alsjeblieft :slight_smile:

Just for the sake of completeness, if it’s truly random, why can’t a LED blink twice in succession ?
Thai kitchen of a coin toss... you might easily get a number of ‘heads’ or ‘tails’ in a row...

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.