How to Generate Random Numbers in Linux

When it comes to the numbers game, no operating system does it better than Linux. Moreover, Linux operating system’s environment is smoothly transparent in its approach. So why would you need to generate random numbers in Linux?

The answer is pretty simple. With the numerous possibility of projects that can be run under Linux, a random-number generator is a must-have skill. For instance, users might be required to key in a unique sequence of characters to authenticate a particular app-related transaction.

The same users might require an application to generate a random and unique password string for them. Also, for a normal Linux user, a random number generator can help create unique file names especially if this user is actively involved in Linux file management.

This article will walk us through various ways of generating random numbers in a Linux operating system.

1. Generate Random Numbers Using shuf Command

As per its manual page, shuf can be used to generate random permutations. The standard syntax for the shuf command is as follows:

$ shuf -i MIN-MAX -n COUNT
  • -i is for the input range.
  • MIN is for the minimum range.
  • MAX is for the maximum range.
  • -n is for the head count.
  • COUNT is for the random number counts e.g 5, 7, 10, etc.

For instance, if we want 12 random numbers in the range of 1000 to 10,000, our shuf command implementation will look like the following:

$ shuf -i 1000-10000 -n 12  
Create Random Numbers in Linux
Create Random Numbers in Linux

By increasing the MIN and MAX range, the width of the generated random number will also increase.

$ shuf -i 100000-1000000 -n 12
Generate Random Numbers in Linux
Generate Random Numbers in Linux

2. Print Random Numbers Using /dev/urandom File

The Linux operating system has a file called /dev/urandom that basically performs pseudorandom number generation.

$ ls -l /dev/urandom
/dev/urandom File in Linux
/dev/urandom File in Linux

We can point this file to other Linux commands to produce random numbers. However, the command we choose to couple with this file should make it easy to specify the range of the random number we wish to generate.

In this case, we can couple the /dev/urandom file with the tr command and then pipe the result to the head command which will specify the length of the random number to be generated.

$ tr -cd "[:digit:]" < /dev/urandom | head -c 13

The tr command only looks for digits in the /dev/urandom file and deletes other characters before piping its result to the head command which counts and prints a random number with 13 digits.

Print Random Number in Linux
Print Random Number in Linux

3. Create Random Numbers Using RANDOM Variable

The Linux shell can generate a random number through a variable called RANDOM. This variable holds a range of numbers between 0 and 32767. Therefore, reading this variable will each time generate a unique random number.

For instance, let us create a bash script that generates a random number by reading the RANDOM variable.

$ nano RundNumGen.sh 

Add the following code to the file.

#!/bin/bash
#LinuxShellTips Script to Generate a Random Number
RANDOM=$$
for i in RANDOM
do
        echo $RANDOM
done

Make the bash script executable and run it.

$ chmod u+x RandNumGen.sh
$ ./RandNumGen.sh

Every time we run the script, a new random number will be generated.

Generate Random Number in Linux
Generate Random Number in Linux

Know of other cool ways to generate random numbers in Linux? Don’t forget to leave a comment.

1 thought on “How to Generate Random Numbers in Linux”

  1. Here are some more ways to generate random numbers in Linux.

    The below command will generate 3 digits random number.

    $ echo $RANDOM | cut -c 1-3
    

    The below command will generate 5 digits random number.

    $ LL=$LANG;LANG=C;L=5;N=;while read -n 1 C; do [[ $C =~ [0-9] ]]&&N+=$C&&((${#N}==L))&&printf '%s' "${N}"&&break;done</dev/urandom;LANG=$LL
    
    Reply

Got something to say? Join the discussion.

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.