Generating Random Number in certain range

Hi there I am trying to generate a random number between 40 and 70 using the shell here is my code so far and it keeps going above 70. all help much appreciated!

comp=$(( RANDOM%70+40 ))
echo $comp
comp=$(( RANDOM%31+40 ))
1 Like

If available on your flavor of 'nix, you can use the shuf command:

shuf -n 1 -i 40-70
1 Like

thank you very much that worked perfectly :slight_smile:

To generalize it:

MIN=10
MAX=20
echo $(( (RANDOM%((MAX-MIN)+1))+MIN))