Random function

hey,
im try to make a random led to go using this code

int randomn,randomn1,randomn2,i;
void setup()
{

  • pinMode(13, OUTPUT);*

  • pinMode(12, OUTPUT);*
    pinMode(11, OUTPUT);
    }
    void loop()
    {

  • for(i=0;i<20;i++){*

  • randomSeed(millis());*

  • randomn = random(11,13);*
    randomn1 = random(11,13);
    randomn2 = random(11,13);

  • digitalWrite(randomn,HIGH);*

  • digitalWrite(randomn1,LOW);*

  • digitalWrite(randomn2,HIGH);*

  • delay(100);*

  • }*
    }

what i have wrong in this code as always the same led is going on
thanks

The application will allways execute the same way so the random seed is always the same.

An often used initializer for randomSeed is analogRead(A0) with A0 connected to nothing. It is still not really random (it will fail on real random tests) but for many applications it is random enough.

Another option is to use a button and use micros()/4 to initialize the random seed if the key is pressed and/or released.

randomn = random(11,13);

Will generate 11 or 12, but not 13.

robtillaart:
An often used initializer for randomSeed is analogRead(A0) with A0 connected to nothing. It is still not really random (it will fail on real random tests) but for many applications it is random enough.

Another option is to use a button and use micros()/4 to initialize the random seed if the key is pressed and/or released.

can you show me the code please as im new to arduino thanks

what i have wrong in this code as always the same led is going on

Several things. You should be generating two random numbers - one for the pin and one for the state - not three.

int randPin = random(11, 14); // Generate value from 11 to 13
int randState = random(0, 2); // Generate value from 0 to 1
digitalWrite(randPin, randState); // Set the pin to the state

Seeding the random number generator would be useful, too.

You generally seed the random number once (outside of the loop). But, that shouldn't be causing you a problem since you are seeding the random generate with a diferent value each time.

I agree that it makes more sense to randomly choose the HIGH/LOW (1/0) state randomly, rather than choosing the pins randomly.

I don't think you need a random number for the pin at all... You can simply write random data to ALL of the pins/LEDs every time through the loop, to pop-up a random pattern each time through the loop (if that's what your're trying to do).

Unless, you wanted to change only one pin each time through the loop. Then, you might want to randomly choose the one-pin to be "active" each time. If you do that, you'll only get a change about half the time, randomly... :wink:

P.S.
I dont know what your goal is, but once you get this working you might want to make one more enhancement...

Since there are 8 different possible "patterns", you are going to get a "repeat" about once per second (randomly)... i.e. you'll get the same pattern twice in a row... so on-average, you'll only get about 9 "changes" per second (instead of the expected 10), and it's going to "look like" a glitch or a pause. If that's a problem, you might want to look for that and skip the delay if you get the same pattern twice in a row.

If there are any other "invalid" states like all-on, or all-off, you can check for that too, and start the loop over if you get something invalid.

can you show me the code please as im new to arduino thanks

Added a function and an array and some #define, several tricks .

int pins[3] = {11, 12, 13};
int randomPin[3];

#define SWITCHPIN 4

int i;

void mySeeder()
{
  if (digitalRead(SWITCHPIN) == LOW) return ;  // only a reseed if the key is pressed
  randomSeed(micros());
}

void setup()
{
  for (int i=0; i< 3; i++) pinMode(pins[i], OUTPUT);

  pinMode(SWITCHPIN , INPUT);     // connect a switch between GND and pin 4 
  digitalWrite(SWITCHPIN , HIGH);  // use internal PULL UP resistor
  
  Serial.begin(115200);
  Serial.println("Press the switch to start ...");

  // first reseed is forced wait until a keypress
  while(digitalRead(4) == LOW);  
  randomSeed(micros());

  Serial.print(millis());
  Serial.println(" - started");
}

void loop()
{
  mySeeder();

  for (int=0; i<3; i++) randomPin[i] = random(11,14);

  digitalWrite(randomPin[0], HIGH);
  delay(30);
  digitalWrite(randomPin[1], LOW);
  delay(30);
  digitalWrite(randomPin[2], HIGH);
  delay(30);
}