Shell – How to Generate Random Numbers in a Specific Range

commandcommand linerandomshell

After googling a bit I couldn't find a simple way to use a shell command to generate a random decimal integer number included in a specific range, that is between a minimum and a maximum.

I read about /dev/random, /dev/urandom and $RANDOM, but none of these can do what I need.

Is there another useful command, or a way to use the previous data?

Best Answer

In the POSIX toolchest, you can use awk:

awk -v min=5 -v max=10 'BEGIN{srand(); print int(min+rand()*(max-min+1))}'

Do not use that as a source to generate passwords or secret data for instance, as with most awk implementations, the number can easily be guessed based on the time that command was run.

With many awk implementations, that command run twice within the same second will generally give you the same output.