A bad way to generate a random init in a range using only bash built-ins. Useful in a pinch when you aren't sure what external programs are available.
# Gets an int between min max inclusive very inefficiently, but | |
# only using bash built-ins. More inefficient the smaller the gap | |
# | |
# usage: n=$( bad_random_int 1000 2000 ) | |
bad_random_int() { | |
local min=$1 | |
local max=$2 | |
local n=0 | |
while [[ "$n" -lt "$min" ]] || [[ "$n" -gt "$max" ]]; do | |
n=$(( $min + $RANDOM )) | |
done | |
echo $n | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment