Random Lights

I'm new to Arduino and am working on a simple project to learn. I want to turn on 4 led lights in a random sequence. I found a thread on the forum where someone else was attempting to do something similar. http://forum.arduino.cc/index.php?topic=132949.0 I copied the code and ran it. It's very close to working but I'm missing something as it will sometimes repeats one of the 4 lights in the sequence instead of blinking all 4. I see in the thread that this is addressed, but I don't completely understand how to fix it. Can someone fix this and repost the code so i can see the corrections?

// Random LED lights code... 

long randNumber1;
int val = 2;
int led1 = 2;
int led2 = 3;
int led3 = 4;
int led4 = 5;


void setup() {                

  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);     
  pinMode(led3, OUTPUT);     
  pinMode(led4, OUTPUT);     
  Serial.begin(9600);
  randomSeed(analogRead(1));
  //make sure pins are on low.
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);


}

// the loop routine runs over and over again forever:
void loop() {
  delay(3000);
  
  val = 1;  
  randNumber1 = random(2,6);
    

//First LED. 
  digitalWrite(randNumber1, HIGH);
  delay(700);
  val = randNumber1;
  digitalWrite(randNumber1, LOW);
  //Serial.println(randNumber1);
  //Serial.println(val);
  
  //Check to see if randNum is same. If so, change it.
  randNumber1 = random(2,6);
  while (randNumber1 == val) {
    randNumber1 = random(2,6);
  } 
  //write to 2nd LED
  digitalWrite(randNumber1, HIGH);
  delay(700);
  digitalWrite(randNumber1, LOW);
  val = randNumber1;


  //Check  
randNumber1 = random(2,6);  
  while (randNumber1 == val) {
    randNumber1 = random(2,6);
  } 
  //Write to 3rd LED 
  digitalWrite(randNumber1, HIGH);
  delay(700);
  digitalWrite(randNumber1, LOW);
    val = randNumber1;

  
  //Check
  randNumber1 = random(2,6);
  while (randNumber1 == val) {
    randNumber1 = random(2,6);
  }

  //Write to 4th LED.
  digitalWrite(randNumber1, HIGH);
  delay(700);
  digitalWrite(randNumber1, LOW);
    val = randNumber1;

  
delay(1000);
}
while (randNumber1 == val) {
    randNumber1 = random(2,6);
  }

You do a good job of checking that the new random number isn’t the same as the last one, but what you really need to do is make sure it isn’t one of the ones you’ve picked before.

Say you get 3, then you get 5. Ok fine, this passes the test because 5 isn’t 3. But then on the next pass you get 3 again. Now this too will pass the test because 3 isn’t 5 and val is 5.

Instead of just keeping up with one previous random number, you’ll have to keep up with at least 3. I would suggest using an array to hold them.

Thanks for the reply. I understand what your saying but as I said i am very new to programming and don't undestand the logic language enough to make this change. Can someone please make this revision to the code provided and repost the entire string so I can compare the changes?

TenorTim:
Thanks for the reply. I understand what your saying but as I said i am very new to programming and don't undestand the logic language enough to make this change. Can someone please make this revision to the code provided and repost the entire string so I can compare the changes?

TenorTim,
if you don't mind, let me show you how to fish - instead of giving you a fish.

First, make this easy on yourself. You are having doubts about the randomness of your numbers. That is, those numbers that are supposed to be random appear to have the LEDs blinking in some sort of sequence.

So remove all your code, and just check the randomness of your numbers.

Do you know how to do that?

Jesse