Dice roll loop issue

Program is supposed to have sevseg at the base of 0, then when the button is pressed display a randomly generated number. Instead it loops until presumably recognized as an infinite loop. I know I need an end case but I'm not sure why what I have isn't working. Any help and optimization is appreciated.

//My Name finished on 10.30.19,
//Sev Seg dice roller
#include "SevSeg.h"
SevSeg sevseg; 
int ran = 0;
const int buttonPin = 11;
int buttonState = 0;
void setup(){
    byte numDigits = 1;
    byte digitPins[] = {};
    byte segmentPins[] = {6, 5, 2, 3, 4, 7, 8, 9};
    bool resistorsOnSegments = true;

    byte hardwareConfig = COMMON_CATHODE; 
    sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments);
    sevseg.setBrightness(90);
}

void loop(){
  buttonState = digitalRead(11);
  while(buttonState == HIGH) {
     ran = random (1,6);
        if (ran == 1) {
            sevseg.setNumber(ran);
            sevseg.refreshDisplay();
        }
        if (ran == 2) {
          sevseg.setNumber(ran);
          sevseg.refreshDisplay();
        }
        if (ran == 3) {
          sevseg.setNumber(ran);
          sevseg.refreshDisplay();
        }
        if (ran == 4) {
          sevseg.setNumber(ran);
          sevseg.refreshDisplay();
        }
        if (ran == 5) {
          sevseg.setNumber(ran);
          sevseg.refreshDisplay();    
        }
        if (ran == 6) {
          sevseg.setNumber(ran);
          sevseg.refreshDisplay();    
        }  
      delay(250);    
  }
  if (buttonState == LOW) {
    sevseg.setNumber(0);
    sevseg.refreshDisplay();
  }
}

buttonState is not updated in the unnecessary while loop, thats why it hangs. And why do you have all those if's - they serve no purpose..

void loop(){

  //Get the state of the button
  static byte lastButtonState = 255;
  byte buttonState = digitalRead(11);
  if (buttonState != lastButtonState) {

    lastButtonState = buttonState;
    delay(50); //Lazy mans debounce

    if (buttonState == HIGH) {
       ran = random (1,6);
       sevseg.setNumber(ran);
       sevseg.refreshDisplay();
       //delay(250); //Do you really need this delay?
    } else {
      sevseg.setNumber(0);
      sevseg.refreshDisplay();
    }
  }
}

What about replacing this code block:

  while(buttonState == HIGH) {
     ran = random (1,6);
        if (ran == 1) {
            sevseg.setNumber(ran);
            sevseg.refreshDisplay();
        }
        if (ran == 2) {
          sevseg.setNumber(ran);
          sevseg.refreshDisplay();
        }
        if (ran == 3) {
          sevseg.setNumber(ran);
          sevseg.refreshDisplay();
        }
        if (ran == 4) {
          sevseg.setNumber(ran);
          sevseg.refreshDisplay();
        }
        if (ran == 5) {
          sevseg.setNumber(ran);
          sevseg.refreshDisplay();   
        }
        if (ran == 6) {
          sevseg.setNumber(ran);
          sevseg.refreshDisplay();   
        } 
      delay(250);   
  }

with:

void ShowANumber(int num)
{
   sevseg.setNumber(num);
   sevseg.refreshDisplay();   
       
   delay(250);   
}

// the original code leading up to the while in loop():

  while(buttonState == HIGH) {
     ran = random (1,6);
     ShowANumber(ran);
  }
  // rest of code

You do need a pinMode() call and probably a debounce, too.

Edit: I forgot to set the random number generator to num instead of ran.

Hi,
this should work, it's a pretty simple sketch:

//My Name finished on 10.30.19,
//Sev Seg dice roller
#include "SevSeg.h"
SevSeg sevseg;
int ran = 0;
const int buttonPin = 11;
int buttonState = 0;
void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
  byte numDigits = 1;
  byte digitPins[] = {};
  byte segmentPins[] = {6, 5, 2, 3, 4, 7, 8, 9};
  bool resistorsOnSegments = true;

  byte hardwareConfig = COMMON_CATHODE;
  sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments);
  sevseg.setBrightness(90);
}

void loop() {
  buttonState = digitalRead(buttonPin);
  if (buttonState == LOW) {   //LOW because it's an INPUT_PULLUP so it's HIGH by default
    ran = random (1, 6);
    switch (ran) {
      case 1: {
          sevseg.setNumber(ran);
          sevseg.refreshDisplay();
          break;
        }
      case 2: {
          sevseg.setNumber(ran);
          sevseg.refreshDisplay();
          break;
        }
      case 3: {
          sevseg.setNumber(ran);
          sevseg.refreshDisplay();
          break;
        }
      case 4: {
          sevseg.setNumber(ran);
          sevseg.refreshDisplay();
          break;
        }
      case 5: {
          sevseg.setNumber(ran);
          sevseg.refreshDisplay();
          break;
        }
      case 6: {
          sevseg.setNumber(ran);
          sevseg.refreshDisplay();
          break;
        }
      default: {
          sevseg.setNumber(0);
          sevseg.refreshDisplay();
          break;
        }
    }
    delay(250);     
  }
}

Obviously, if you are pressing the button continiously, it will print every 250ms the "ran" number.

I don’t know if i can/how i should reply but thanks everyone and especially CyberBarby. I don’t usually feel right using an answer that goes way beyond what I understand but thankfully I just forgot switch cases existed. Thanks for the answers yall <3