Generating Random Numbers in Bash
In Bash, you can use either integers or floating point numbers to generate random numbers. A bash script can be used to create random numbers within a given range or size. This article demonstrates various ways to generate random numbers in Bash.
$RANDOM
Generate random numbers using variables
$RANDOM
A variable can be used to generate random numbers or a range of random values between 0
and 32767
. However, you can $RANDOM
limit the range of numbers used to generate random numbers by dividing the value by a specific value.
To generate a random number between 0 and 32767, use the following command:
$ echo $RANDOM
Output:
28091
$RANDOM
You can generate random numbers from a specific range by dividing the variable by the remainder value. It contains $
double brackets with a (())
.
To generate a random number between 1 and 100, use the following command:
$ echo $(( $RANDOM % 100 + 1 ))
Output:
19
Since the percent sign causes the mathematical operation modulo to fail, the number 100 (the remainder after the division) will never be reached. If the random generator returns the value 100, the modulo operation will provide the value 0.
Use shuf
the command to generate random numbers
You can shuf
generate large numbers in shell scripts using the command.
The following command creates a single integer between 0 and 100:
$ shuf -i 0-100 -n1
Output:
76
In the above command, -i
represents the input range, -n
represents the number of people. The key -n
followed by any positive number represents the number of random numbers generated. Run the following command to print five random numbers between 0 and 100.
$ shuf -i 0-100 -n5
Output:
68
78
24
76
41
/dev/urandom
Generate random numbers using
/dev/urandom
Device files allow you to generate $RANDOM
pseudo-random numbers that are more random than variables. However, getting those numbers into variables in your script requires further work, such as od
filtering through , as shown in the example.
od /dev/urandom -A n -t d -N 1
-t d
The option indicates that signed decimal should be used as the output format, -N 1
telling the program to /dev/urandom
get a byte from .
Output:
124
Generate random numbers using awk
awk
The rand()
function returns a random floating point number in the range 0-1. BEGIN
The following command will be executed before the next step.
awk 'BEGIN{x=rand(); print x}'
Output:
0.444937
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Run batch (.bat) files in CMD
Publish Date:2025/03/20 Views:155 Category:OPERATING SYSTEM
-
This article will show you how to use CMD to run a batch file.bat. There are three ways to run a batch file. Let us discuss them in the following sections. Run batch (.bat) files in CMD by directly clicking on them This way you can just go
Changing CMD text color using batch script
Publish Date:2025/03/20 Views:171 Category:OPERATING SYSTEM
-
This article will first discuss the basic concepts of batch scripts or batch files. After introducing the Batch script, let's discuss how to use the Batch script to change the text color of CMD every second. Batch script or file A batch scr
Batch set command timeout
Publish Date:2025/03/20 Views:50 Category:OPERATING SYSTEM
-
This article will first discuss the concept of timeout command in batch scripting. After that, we will discuss setting timeout command for any other command. Timeout command in batch script A timeout is a utility that pauses or delays for a
Batch merge XML files
Publish Date:2025/03/20 Views:101 Category:OPERATING SYSTEM
-
This article will first discuss and understand the XML file format. After that, we will discuss merging two or more XML files into one file using batch commands and scripts. XML File XML, also known as Extensible Markup Language, is a marku
Run .exe file from command prompt using batch script
Publish Date:2025/03/20 Views:76 Category:OPERATING SYSTEM
-
This article will show you how to use a batch (.bat) script to run a .exe type file. You can use two different commands to achieve this. Let’s discuss each method in the following sections. Run .exe file from command prompt using title an
Deleting files older than N days using batch script
Publish Date:2025/03/20 Views:57 Category:OPERATING SYSTEM
-
In this article, we will use a batch script to delete files older than N days. Deleting files older than N days using batch script The general format of the code to perform this task is shown below. FORFILES /p "D:\DIRECTORY" /S /M *.* /D -
Deleting files using batch script
Publish Date:2025/03/20 Views:178 Category:OPERATING SYSTEM
-
This article will demonstrate how to delete files using a batch script. Deleting files using batch script Generally, we can easily delete files by clicking on delete or pressing the delete button on the keyboard. But in Batch, we have to fo
Concatenate multiple files using batch script
Publish Date:2025/03/20 Views:195 Category:OPERATING SYSTEM
-
In this article, we will see how to concatenate multiple files into one file. Concatenate multiple files using batch script The general format of the commands we will use is shown below. type FileONE.txt FileTWO.txt ConcatFile.txt There are
Check if a file exists using batch processing
Publish Date:2025/03/20 Views:100 Category:OPERATING SYSTEM
-
This article will demonstrate how to use a batch script to check if a file exists through sample code. Check if a file exists using batch script The general format or syntax of the code to check if a file exists is provided below. IF EXIST