Created
July 11, 2019 13:33
-
-
Save timruffles/48db3ee3ace07abfed7dee42cc5156c6 to your computer and use it in GitHub Desktop.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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