RANDOM is a peculiar shell variable, but useful nonetheless. Peculiar because its value changes each time it is referenced (yes, this is by design).
As you may have already guessed, RANDOM is a random number generator. The number generated is an integer between 0 and 32767, and can come in handy when writing shell scripts. To determine if a shell you’re using supports this variable, the following command can be used:
$ print $RANDOM $RANDOM
Two different numbers will be displayed if it’s supported, otherwise you will see nothing.
Assigning a numeric value to RANDOM prior to referencing it will initialize (seed) the sequence of random numbers:
Just to get you thinking about potential uses for this handy variable, consider the following…
When writing a shell script to automate the process of adding new user accounts, it may be desirable to generate a unique initial password for each account. Using the value provided by RANDOM for all or part of the password would accomplish this. It would be wise to use the PID of the process creating the account(s) to seed the generator: