How to Create Random Pauses?

I just got my Arduino last night and have been playing all day. I have the Seedstudio Relay Shield v.1.3 (darn you, Radio Shack, there exists a version 2), and have successfully programmed the Arduino to operate the relays. Essentially I'm turning the relays on & off with digitalWrites and delays. My primitive code is included below.

My goal is the following:
I have a vintage marionette coin-op game that has 4 buttons; 2 for the hands and 2 for the feet. The operator pushes the buttons to make him dance to the music. The buttons activate 4 separate solenoids. I plan to use the Relay Shield to operate the solenoids without human intervention.

Therefore, the 4 solenoids need to be activated/deactivated somewhat randomly to simulate human operation. The activation time for each would be APPROX. .75 second, with some variation there. When people operate this puppet they usually just flail around and don't keep time with the music, therefore I don't have to sync it with the music. I simply need the hands and feet to go up & down somewhat randomly.

Can someone steer me in the right direction to help me generate this random operation? Thanks for any help. Here's what I've done so far:

void setup() {                
  // initialize the digital pins as an output.
  
  pinMode(7, OUTPUT);  
  pinMode(6, OUTPUT); 
  pinMode(5, OUTPUT); 
  pinMode(4, OUTPUT);    
}

void loop() {
  digitalWrite(7, HIGH);   // Turns ON Relay on digital pin 7
  delay(750);              // wait for a period
  digitalWrite(6, HIGH);   // Turns ON Relay on digital pin 6
  delay(750);              // wait for a period
  digitalWrite(5, HIGH);   // Turns ON Relay on digital pin 5
  delay(750);              // wait for a period
  digitalWrite(4, HIGH);   // Turns ON Relay on digital pin 4
  delay(750);              // wait for a period

  digitalWrite(4, LOW);    // Turns OFF Relay on digital pin 4
  delay(750);              // wait for a period
  digitalWrite(5, LOW);    // Turns OFF Relay on digital pin 5
  delay(750);              // wait for a period
  digitalWrite(6, LOW);    // Turns OFF Relay on digital pin 6
  delay(750);              // wait for a period
  digitalWrite(7, LOW);    // Turns OFF Relay on digital pin 7
  delay(750);              // wait for a period
}

Use the random function to generate your randomness. Take a look at the blink without delay example and play around with setting the interval variable each time through loop with the random function so the led flashes at varying intervals. Then use random again to calculate a number between 4 & 7. Use this number to decide which solenoid to activate/deactivate.

Thanks, wildbill. I took your suggestion, and read up on the random function, and I think I cleaned up my code fairly well.

I'm experimenting with LEDs before actually installing it into the puppet, and from what I can see with the LEDs I should have a very human-looking operation.

I first created a program where only 1 solenoid would activate at a time. This was no good because I see that humans frequently press two buttons at a time to operate the puppet. Therefore I copied the code and made two separate sets of variables to accomodate multiple button pressing. It's probably not as elegant as it should be but it seems to work. I only started programming last night and I never expected to get this far so soon, but naturally I'm too inexperienced to know just how inefficient my code really is.

My only real concern here is that in the code that chooses which relay to fire, instead of random(4,8) I used random(1,8). I did this to make sure that sometimes only one relay fires, again to simulate how a human operates the puppet. If I'm not mistaken, I'm wasting 3 digital inputs by including them in the random statement. I would like to have them available for other functions. Is there a way to go back to random(4,8) and still ensure that sometimes only one relay fires at a time? Thanks a million! Code below:

// This sketch is intended to automate Peppy the Musical Clown's buttons.
// It is designed to simulate operation by a human.

void setup() {                
  // initialize the digital pins as outputs:
  pinMode(7, OUTPUT);  
  pinMode(6, OUTPUT); 
  pinMode(5, OUTPUT); 
  pinMode(4, OUTPUT);    
}

void loop() 

  // The following code allows for 2 solenoids to be occasionally activated simultaneousely, similar to human operation:
{ 
  int randomSol = random(1,8);        // Choose a random solenoid between 1 and 4 allowing for three possible zeros
  int randomSol2 = random(1,8);       // Choose a random solenoid between 1 and 4 allowing for three possible zeros
  int buttonDown= random(150,400);    // Generate a random button down time for solenoid 1
  int buttonDown2= random(150,400);   // Generate a random button down time for solenoid 2
  int delayOff= random(20,50);        // Generate a random button off time for solenoid 1
  int delayOff2= random(20,50);       // Generate a random button off time for solenoid 2
  int del= random(50,150);            // Generate short random delay for humanization
  int del2= random(50,100);
  
    digitalWrite(randomSol, HIGH);    // Activate randomly chosen solenoid 1
    delay(del);                       // Humanize delay so that simultaneous activations are offset slightly
    digitalWrite(randomSol2, HIGH);   // Activate randomly chosen solenoid 2
    delay(buttonDown);                // How long solenoid 1 will be activated
    delay(buttonDown2);               // How long solenoid 2 will be activated
    digitalWrite(randomSol, LOW);     // Turn randomly chosen solenoid 1 off
    delay(del2);                      // Humanize button release
    digitalWrite(randomSol2, LOW);    // Turn randomly chosed solenoid 2 off
    delay(delayOff);                  // Delay before looping
    delay(delayOff2);                 // Delay before looping
}

Tricky to do in an elegant way without a major restructure of your code. For a brute force approach though, you can use an if statement controlling each digitalRead to ensure that the pin number you're about to use is greater than 3.

Found this code on internet. Hope it may help you.

/*
Advanced Blink Sketch
By: David M. Orlo
www.DaviedOrlo.com
*/
byte SWITCHPIN= 2; //Set Pin 2 as Switch
byte LEDPIN = 6; //Set Pin 6 as LED
byte brightness; //Create a Integer variable named brightness
byte delayedoff; //Create a Integer variable named delayedoff
byte delayedon; //Create a Integer variable named delayedon
//If you want to go higher than 255 you must change from "byte" to "int"
boolean buttonstate; //Create a Integer variable named buttonstate
 
void setup()
{
pinMode(SWITCHPIN, INPUT); //Set Pin 2 as Input
pinMode(LEDPIN, OUTPUT); //Set Pin 6 as Output
}
 
void loop()
{
buttonstate = digitalRead(SWITCHPIN); //Continually look at the switch to see if its pressed
if (buttonstate == HIGH) //If the switch goes HIGH, act on it
{
crazyLED(); //Now we go into a new function called crazyLED
}
}
 
void crazyLED()//crazyLED function we created and called whatever we want
{
buttonstate = LOW; //First we tell the micro the switch is now LOW
delay(250);
while (buttonstate == LOW) //White the switch is NOT pressed we do the following
{
buttonstate = digitalRead(SWITCHPIN); //Continually look at the switch to see if its pressed
brightness = random(1, 254); //Generates a random number from 1-254 and assigns it to the 
variable named brightness
delayedoff = random(1, 125); //Generates a random delay and assigns it to the variable named 
delayed
delayedon = random(1, 250); //Generates a random delay and assigns it to the variable named 
delayed
analogWrite(LEDPIN, brightness); //Uses the random number we generated to write the value to 
our LED
delay(delayedon); //random delay on time
analogWrite(LEDPIN, 0); //We turn the LED off for a blinking effect
delay(delayedoff); //Random delay off time
}
//Once
 the switch is pressed again we break out of the loop above and then break out of the function completely and go back to our main loop
buttonstate = LOW; //First we tell the micro the switch is now LOW
analogWrite(LEDPIN, 0); //We turn the LED off before leaving our custom function
delay(500);
}