Generating random numbers to match a range in Bash
The idea is to set the range at the lower border by default, for example 10
with a higher border, for example 45
. So you set up the bottom border like this: $RANDOM % 45 + 10
right?
But there is a problem with this solution, it assumes that you will always be between 0 + 10
and 45
, so it actually works until you reach 35 ( 35 + 10 = 45
your upper bound), more than 35
outside of your bounds.
The solution for staying in the range is to do this $RANDOM % (higher_b - lower_b)
, which will allow you to stay at the top border, and then add a bottom border which gives you:
$RANDOM % (45 -10) + 10
An example of incorrect output:
for i in {0..10};do printf $[RANDOM % 45 + 10]" ";done
47 31 53 23 36 10 22 36 11 25 54
An example of the 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 a higher score.
source to share
The problem with modulo is that $RANDOM % N
if N is not a power of 2, it does not have an equal probability distribution for all results: How to generate a random number in Bash? ... Per man bash
, $RANDOM
produces a number between 0 and 32767 (2 ** 15-1). This may not matter much in some situations, but by slightly rewriting the expression, we get an equal distribution.
for i in {0..10}; do echo -n "$((RANDOM*36/32768 + 10)) "; done; echo
A bash script with a user selectable range:
#!/bin/bash
LOW=$1
HIGH=$2
echo $((RANDOM * ($HIGH-$LOW+1) / 32768 + LOW))
You will also want to do some parameter checking.
source to share