Timeout for while loop

I have a while loop (below) that uses a runif function to generate random variables that are used in further calculations and conditions before exiting the loop. However, in few cases, this gets stuck in infinite loop as the random number generation does not meet the condition soon.

Is there a way i can include a timeout clause here so that my while loop exits after n seconds?

#lb,ub are user inputs

counter = 1

while(counter == 1){
  
  temp = runif(3,min=0,max=80)
  temp2 = 100 - sum(temp)
  
  if (temp2 > ub || temp2 < lb) {
    
    counter = 1
  
    } else counter = 0
}

I've never tried this before, but why not something like this?

start_user_time <- proc.time()[["user.self"]]

while (TRUE | ((proc.time()[["user.self"]] - start_user_time) <= 10)) {
  x <- 1
}

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.