[SOLVED] Random LED's

Grumpy_Mike:

sometimes there will be only 1 or 2 light burning instead of 3, because the pin has already been picked.

So instead of just picking a random number and using it you pick a random number and if it has already been chosen pick another one. A while loop can be used to ensure that it only exits when a number not chosen before has been selected.

Like this:

int timer = 1000; //Sets timer
const int ledPins [] = {
  2, 3, 4, 5, 6, 7,}; //Sets pins
const int pinCount = 6; //Sets the number of pins
int Number = 3; //Sets the amount of LEDs that will go on
int thisPin;
int alreadySet;

void setup() {
  Serial.begin(9600);
  for (int thisPin = 0; thisPin < pinCount; thisPin++) {
    pinMode(ledPins[thisPin], OUTPUT); //Sets all the ledPins as output
  }
}

void loop(){
  // Turn off all the LEDs
  for (int thisPin = 0; thisPin < pinCount; thisPin++)
    digitalWrite(ledPins[thisPin], LOW);

  // Turn on "Number" randonm LEDs
  for (int i=0; i<Number; i++) {
    do {
      thisPin = random(pinCount);
      alreadySet = digitalRead(ledPins[thisPin]);
      digitalWrite(ledPins[thisPin], HIGH); 
    } 
    while (alreadySet);  // If the LED was already on, try a different one
  }

  delay(timer);
}