Skip to content

Instantly share code, notes, and snippets.

@cmndrsp0ck
Created May 23, 2018 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmndrsp0ck/1bcb6dc3c2f0b0a985092390a8b4a2f7 to your computer and use it in GitHub Desktop.
Save cmndrsp0ck/1bcb6dc3c2f0b0a985092390a8b4a2f7 to your computer and use it in GitHub Desktop.
Generate random number of variable digit length
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