Categories: TutorialsUbuntu

Generating Random Numbers From Linux Terminal

Generating random numbers is a fun kind of work to do. Sometimes, such tasks are needed for a purpose too like creating a password. In Linux, there are several ways to generate random numbers with some scripts.

In this article, we are going to explain the different ways to generate random numbers through Linux terminals.

Use of $RANDOM variable

One way is to use the $RANDOM variable to generate random numbers within the range of 0 and 32767. It is a builtin shell variable to generate the random number. To create random numbers, run the command as shown below.

$ echo $RANDOM

Generate
Sponsored
random number between the chosen range

With the small tweak on a command, you are able to generate random numbers between the chosen range. To generate a random number between the range 0 to 30, you have to run the command as shown below.

$ a=$(( $RANDOM % 31 ))
$ echo $a

Another way to generate a random number between 0 to 100, you have to run the command as shown below.

$ echo $(( $RANDOM % 101 ))

Generate random number using the script

With the use of bash scripting, random numbers can also be easily generated. It is also a bit fun to write the script for such a purpose. Simple example of a script to generate a random number is shown below.

$ sudo vim randomnumber.sh

Sponsored
#!/bin/bash

echo "Enter the smallest number:"

read smallest

echo "Enter the largest number:"

read largest

if [[ $largest 

Output:

With such a script, you are able to generate random numbers between the range provided by you.

Use of shuf command

It is also one of the ways to generate random numbers between the ranges. Run a simple command on the Linux terminal for such a purpose. To generate the random number between the range 7 and 57, run the command as shown below.

$ shuf -i 7-57 -n1

Use of tr command with urandom

To generate the random number with “n” number of digits, you can pull numbers from the /dev/urandom stream with the use of tr command. Suppose to generate a random number with 3 and 7 digits, you have to run the command as shown below.

$ tr -cd "[:digit:]" 
$ tr -cd "[:digit:]" 

Conclusion

In this article, you have learnt to generate random numbers with a small command on the terminal or by creating the script. Also, you learnt to create random numbers between the range as provided. Thank you!

Ubuntu Server Admin

Recent Posts

Canonical Releases Ubuntu 25.04 Plucky Puffin

The latest interim release of Ubuntu introduces “devpacks” for popular frameworks like Spring, along with…

2 days ago

Ubuntu 25.04 (Plucky Puffin) Released

Ubuntu 25.04, codenamed “Plucky Puffin”, is here. This release continues Ubuntu’s proud tradition of integrating…

3 days ago

Extended Security Maintenance for Ubuntu 20.04 (Focal Fossa) begins May 29, 2025

Ubuntu released its 20.04 (Focal Fossa) release 5 years ago, on March 23, 2020. As…

3 days ago

Ubuntu 20.04 LTS End Of Life – activate ESM to keep your fleet of devices secure and operational

Focal Fossa will reach the End of Standard Support in May 2025, also known as…

4 days ago

Ubuntu MATE 25.04 Release Notes

Ubuntu MATE 25.04 is ready to soar! 🪽 Celebrating our 10th anniversary as an official…

5 days ago

Ubuntu Weekly Newsletter Issue 887

Welcome to the Ubuntu Weekly Newsletter, Issue 887 for the week of April 6 –…

6 days ago