Random integers in BASH

Never really had a need for it before in BASH, but the following formula allows you to create random integers between two values:

rnumber=$(((RANDOM%(max-min+divisibleBy))/divisibleBy*divisibleBy+min))

Useful for example to create random passwords that have a length within a set range.

< /dev/urandom tr -dc A-Za-z0-9_$*?!.:=@#% | head -c$((RANDOM %4 +8))

You can leave a response, or trackback from your own site.

Leave a Reply