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
By increasing the MIN and MAX range, the width of the generated random number will also increase.
$ shuf -i 100000-1000000 -n 12
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
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.
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.
Know of other cool ways to generate random numbers in Linux? Don’t forget to leave a comment.
I’m going to be upfront and honest with you. I’m 77 years old and trying to make an extra dollar using the Oklahoma Pick-3 lottery. How would I link this command line (
echo $RANDOM | cut -c 1-3
) to a source file of previously picked 3-digit numbers? Or perhaps something else mentioned in the article.@Charley
You can link the command to a source file by using grep or awk to check if the generated number matches any in the file.
This will compare the generated number with entries in the file.
Here are some more ways to generate random numbers in Linux.
The below command will generate 3 digits random number.
The below command will generate 5 digits random number.