$RANDOM in sleep

I'm trying to get my script to pause randomly between 20 and 10 seconds where the max amount of time is 20 and minimum 10. I'm using this atm:

sleep $((10 + RANDOM % 20));

But the problem is that it disregards the limit of 20 and goes beyond that. Is there an other way?

1

1 Answer

10 and 20 are not min and max of the RANDOM number. The expression is simple math. % means modulo.

You want

sleep $((10 + RANDOM % 11));

It's 11 because 10-20 are 11 numbers.


Alternatively, you could use shuf:

sleep $(shuf -i 10-20 -n 1)
2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like