The idea is to set your range at a default lower bound, say 10
, with a higher bound, say 45
. So you adjust the lower bound like this : $RANDOM % 45 + 10
, don't you?
But there is a problem with this solution, it assumes that you'll always be between 0 + 10
and 45
so in fact it works until you reach 35 (35 + 10 = 45
your higher bound), anymore than 35
will be out of your bounds.
The solution in order to stay in the range is to do $RANDOM % (higher_b - lower_b)
which will allow you to stay in higher bound then to add lower bound which gives you :
$RANDOM % (45 -10) + 10
example wrong output:
for i in {0..10};do printf $[RANDOM % 45 + 10]" ";done
47 31 53 23 36 10 22 36 11 25 54
example right output:
for i in {0..10};do printf $[RANDOM % 35 +10]" ";done
39 44 14 12 38 31 25 13 42 33 16
You can also write RANDOM % (higher - lower +1)
if you want your index to include higher bound.