Created
May 23, 2018 17:28
-
-
Save cmndrsp0ck/1bcb6dc3c2f0b0a985092390a8b4a2f7 to your computer and use it in GitHub Desktop.
Generate random number of variable digit length
This file contains 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
function randNum() { | |
# Generate random number of variable length with a default of 5 digits | |
local val | |
local len=${1:-5} | |
for (( x=0; x<${len}; x++ )); do | |
val+=($(echo $((RANDOM%9)))); | |
done | |
printf -- "%s" "${val[@]}" | |
} | |
# first digit can be 0 (zero) | |
# has a default of 5 digits if no args passed | |
# output should be stored to variable | |
# examples | |
some_num=$(randNum); # will output a 5 digit value and store to $some_num | |
other_num=$(randNum 10); # will output a 10 digit value and store to $other_num |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment