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.
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
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 ))
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
#!/bin/bash echo "Enter the smallest number:" read smallest echo "Enter the largest number:" read largest if [[ $largest < $smallest ]]; then echo "Your provided numbers is not valid" exit 1 fi change=$(( $largest-$smallest )) if [[ $change == 1 ]]; then echo "Range between the numbers must be more than 1" exit 1 fi randomnumber=$(( $RANDOM % $largest + 1)) echo "The random number is: $randomnumber"
Output:
With such a script, you are able to generate random numbers between the range provided by you.
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
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:]" < /dev/urandom | head -c 3
$ tr -cd "[:digit:]" < /dev/urandom | head -c 7
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!
Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications including CCNA RS, SCP, and ACE. As an IT engineer and technical author, he writes for various websites.
Photo by Jeton Bajrami on Unsplash Date: December 4-5th, 2024 Location: Geneva, Switzerland In just…
Who will win the race to the perfect SDV? The automotive industry is experiencing a…
Software developers spend a huge amount of effort working on optimization – extracting more speed…
Welcome to the Ubuntu Weekly Newsletter, Issue 866 for the week of November 10 –…
Debian and Ubuntu are two popular Linux distributions. In this deep dive we will guide…
In this article, we will see how to Install Google Cloud BigQuery Python client library…