My random servo programming updates at the wrong time

(deleted)

The state change detection example may be what you need. With it you look for the transitions (low to high or high to low) instead of the state (is low or is high).

(deleted)

Something like this? Although, because of the for loops blocking, it is not at all responsive. I changed the button wiring to an active low button*. Button wired to ground and an input. Input set to pinMode INPUT_PULLUP (internal pullup enabled). Button state is LOW when pressed.

#include <Servo.h>
Servo myservo;
const int buttonPin = 3;     // the number of the toggle switch pin
int pos = 0;
// variables will change:
int buttonState = 0;         // variable for reading the toggle switch status
long ran;

bool lastButtonState = HIGH; // *** store old button state

void setup()
{
   myservo.attach(5);
   Serial.begin(9600);
   pinMode(buttonPin, INPUT_PULLUP);
   randomSeed(analogRead(2));
}

void loop()
{
   // read the state of the toggle switch value:
   buttonState = digitalRead(buttonPin);
   if (buttonState != lastButtonState)  // *** if button state has changed
   {
      if (buttonState == LOW) // ** if button went from not pressed to pressed (falling edge).
      {
         ran = random(1, 4);
         Serial.println(ran);
      }
      lastButtonState = buttonState; // ** save old value for next time
   }

   if (buttonState == HIGH && ran == 1)
   {
      // the servo i'm using is very limited(I think its somewhere around 160 degrees of motion), so
      //it is programmed to move back and forth

      for (pos = 0; pos <= 100; pos += 1)
      {

         myservo.write(pos);
         delay(11);
      }
      for (pos = 100; pos >= 0; pos -= 1)
      {
         myservo.write(pos);

         delay(11);
      }
   }

   else if (buttonState == HIGH && ran == 2)
   {
      for (pos = 0; pos <= 100; pos += 1)
      {

         myservo.write(pos);
         delay(7);
      }
      for (pos = 100; pos >= 0; pos -= 1)
      {
         myservo.write(pos);
         delay(7);
      }
   }
   
   else if (buttonState == HIGH && ran == 3)
   {
      for (pos = 0; pos <= 100; pos += 1)
      {

         myservo.write(pos);
         delay(3);
      }
      for (pos = 100; pos >= 0; pos -= 1)
      {
         myservo.write(pos);
         delay(3);
      }
   }
}