Get a random number on Linux ? 

The bash has it, the bash shell offers $RANDOM variable. From the bash man page:

    Each time this is referenced, a random integer between 0 and 32767 is generated. The sequence of random numbers may be initialized by assigning a value to RANDOM. If RANDOM is unset, it loses its special properties, even if it is subsequently reset.

Generat a random number

To view $RANDOM, enter:

$ echo $RANDOM   #Sample outputs:

You can store it to a variable for later use:

 echo "My move: trying $n..."

You can have a random integer number within a certain range

# display a random integer <= 1000
echo $(( r %= 1000 ))

# display random number between 100 and 1000.
echo $((RANDOM%1000+100))

There are also some interesting cases, see How to randomly pick some lines from a files


Comments powered by CComment