Random Relays

I'm trying to completely randomize the relays being triggered. I have found out how to randomize the amount of time between on and off but its still only 1 relay at a time. My ultimate goal is to have it randomly turn on anything from 1 to 4 relays for a random duration (say 1 second to 10 seconds). I would also like to randomize the duration PER relay rather than all relays at once (which I am assuming would be easier).

{
   digitalWrite(RELAY1,LOW);
   digitalWrite(RELAY2,LOW);           // Turns ON Relays 1
   delay(random(100,4000));                                      // Wait 2 seconds
   digitalWrite(RELAY1,HIGH);          // Turns Relay Off
   digitalWrite(RELAY2,HIGH);          // Turns Relay Off
 
   digitalWrite(RELAY2,LOW);           // Turns ON Relays 2
   delay(random(100,4000));                                      // Wait 2 seconds
   digitalWrite(RELAY2,HIGH);          // Turns Relay Off
 
   digitalWrite(RELAY3,LOW);           // Turns ON Relays 3
   delay(random(100,4000));                                      // Wait 2 seconds
   digitalWrite(RELAY3,HIGH);          // Turns Relay Off
 
   digitalWrite(RELAY4,LOW);           // Turns ON Relays 4
   delay(random(100,4000));                                      // Wait 2 seconds
   digitalWrite(RELAY4,HIGH);          // Turns Relay Off        
 }

I'm using a code that like currently. Any suggestions would be appreciated!

Take a look at this thread. several things at a time

The 'several things at a time' is one (good) solution.

But...

You just want to switch all relays randomly on or off, independently, right?

byte relays[4] = { 3, 4, 5, 6 };  // use YOUR pins

void setup() {}

void loop() {
  digitalWrite(relays[random(0, 4)], random(0, 2));
  delay(random(50, 2000));
}

Too 'quick and dirty'?

@Whandall Not sure what that is meant to do but it doesnt seem to switch any of the relays at all.

Here is the full code I was using:

// Basic 4 Realy board connection
// Each relay is turned on for 2 seconds and then off.
// You can here them click as there state changes from off to on and on to
// off.
// You will also see the corresponding Red LED on the 4 Relay board
// light up when the relay is on.
 
 //  define names for the 4 Digital pins On the Arduino 7,8,9,10
 //  These data pins link to 4 Relay board pins IN1, IN2, IN3, IN4

#define RELAY1  6                        
#define RELAY2  7                        
#define RELAY3  8                        
#define RELAY4  9
 
void setup()
{    
// Initialise the Arduino data pins for OUTPUT
  pinMode(RELAY1, OUTPUT);       
  pinMode(RELAY2, OUTPUT);
  pinMode(RELAY3, OUTPUT);
  pinMode(RELAY4, OUTPUT);
}
 
 void loop()
{
   digitalWrite(RELAY1,LOW);
   delay(random(1000,10000));                                      // Wait 2 seconds
   digitalWrite(RELAY1,HIGH);          // Turns Relay Off
 
   digitalWrite(RELAY2,LOW);           // Turns ON Relays 2
   delay(random(1000,10000));                                      // Wait 2 seconds
   digitalWrite(RELAY2,HIGH);          // Turns Relay Off
 
   digitalWrite(RELAY3,LOW);           // Turns ON Relays 3
   delay(random(1000,10000));                                      // Wait 2 seconds
   digitalWrite(RELAY3,HIGH);          // Turns Relay Off
 
   digitalWrite(RELAY4,LOW);           // Turns ON Relays 4
   delay(random(1000,10000));                                      // Wait 2 seconds
   digitalWrite(RELAY4,HIGH);          // Turns Relay Off        
 }

The code you posted last has a different timing than the OT.

So you did not try my code, because it didn't look 'right'?

It does what you wanted to do, even so you can not see it by looking at the code.

It will write a random value to a randomly picked relay pin and then wait a random time.
So the relays will switch on and off at random times.

#define RELAY1  6
#define RELAY2  7
#define RELAY3  8
#define RELAY4  9
byte relays[4] = { RELAY1, RELAY2, RELAY3, RELAY4 };
void setup() {
  pinMode(RELAY1, OUTPUT);
  pinMode(RELAY2, OUTPUT);
  pinMode(RELAY3, OUTPUT);
  pinMode(RELAY4, OUTPUT);
}
void loop() {
  digitalWrite(relays[random(0, 4)], random(0, 2));
  delay(random(500, 5000));
}