random not very random

I am using this to randomly pick a pin from my array to turn them on/off...but it is not very random at all. In fact it is quite predictable. Is there a better approach?

const byte servoPins [] = {2, 4, 7, 9};
int restart = 0;
int rndmServo = 0;

void setup() {
  // set pins as output
  pinMode(2, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(9, OUTPUT);

}

void loop() {
  // if restart var == 0
  if(restart == 0){
  

    // start 10 run loop
    for (int i = 0; i < 10; i++) {
       // pick random
       rndmServo = servoPins [random (0, 4)];
       // move to 90
       digitalWrite(rndmServo, HIGH); // turn the LED on (HIGH is the voltage level)
       // pause
       delay(1000); // wait for a second
       // move to 0
       digitalWrite(rndmServo, LOW); // turn the LED off by making the voltage LOW
       // pause
       delay(1000);  // wait for a second
       // end 10 run loop
    }
    // reset restart var = 1
    restart = 1;
    
    // end if restart var == 0
  }
}

... this is eventually going to be for a bank of servos(hence the reference to servos), but I am just trying with led's to keep it simple for now

http://www.arduino.cc/en/Reference/RandomSeed

A floating ADC is often used as a seed.

But yeay, true random is very very hard. In digital logic it's better to settle for a pseudo random (like the Arduino random).

http://forum.arduino.cc/index.php?topic=66206.0
http://forum.arduino.cc/index.php?topic=66206.msg537783#msg537783

Another possible technique is to wait for a button press and use the value of millis() or micros() when the button is pressed as the parameter for randomSeed()

Predictable? or the same each run?

http://c-faq.com/lib/randrange.html

The variable rndmServo is not defined anywhere that I can find. How can this program be analyzed without this information?

@vaj4088 ..
oops, I must have forgotten to paste that line here.... it was

int rndmServo = 0;

I will edit the op, and I will look into that random seed

and I am always getting the same sequence of numbers on reset

gruant2000:
@vaj4088 ..
oops, I must have forgotten to paste that line here.... it was

int rndmServo = 0;

I will edit the op, and I will look into that random seed

and I am always getting the same sequence of numbers on reset

random() does not generate true random numbers. It generates pseudo-random sequence with a random (high entropy) distribution of output values. if it always gets the same seed, it will always generate the same sequence. If you want a different sequence each time your program runs, then you need to give it a different seed each time you run. This is usually done by basing the seed on some random natural event, like the response time of the user, or the time between your program starting, and some external event that is not timed off your program starting. Computers do not have true random number generators.

Regards,
Ray L.

Thank you for the Random Seed info.
This is now behaving more as I wanted

// servo pin array
const byte servoPins [] = {2, 4, 7, 9};
int rndmServo = 0;
// restart var = 0
int restart = 0;
long randNumber;

void setup() {
  // set pins as output
  pinMode(2, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(9, OUTPUT);
  Serial.begin(9600);
  randomSeed(analogRead(0));

}

void loop() {
  if(restart == 0){
    for (int i = 0; i < 10; i++) {
       randNumber = servoPins [random (0, 4)];
       Serial.println(randNumber);
       delay(50);
    }
    restart = 1;
  }
}