Bash

Random number from a range in a Bash Script

25 September 2026 · 5 min read

Random number from a range in a Bash Script

Generating random numbers within a specific range is a fundamental task in bash scripting, often used for simulations, testing, and various automation processes. Understanding how to effectively create and utilize these random numbers can significantly enhance your scripting capabilities. This article delves into the methods for generating random numbers in bash, exploring different techniques and best practices for achieving specific outcomes. We’ll cover everything from simple integer generation to creating random strings and floating-point numbers.

Generating Basic Random Integers

Bash provides the built-in $RANDOM variable for generating pseudo-random integers. This variable produces a random integer between 0 and 32767 inclusive. While suitable for basic scenarios, this limited range might not always suffice.

To restrict the random number to a specific range, the modulo operator (%) is key. For instance, to generate a random number between 0 and 9, you would use $((RANDOM % 10)). This expression calculates the remainder when $RANDOM is divided by 10, effectively limiting the result to the desired range. This technique is simple and efficient for generating random numbers within relatively small ranges.

For larger ranges, you might encounter limitations due to the maximum value of $RANDOM. However, combining multiple $RANDOM calls and arithmetic operations can expand the attainable range significantly. This approach allows for greater flexibility when generating random numbers for various applications.

Specifying Custom Ranges

Generating random numbers within a specific, non-zero starting range requires a slight modification to the previous technique. Let’s say you need a random number between 10 and 20 (inclusive). The formula becomes $(( (RANDOM % 11) + 10 )). Here, RANDOM % 11 generates a number between 0 and 10. Adding 10 shifts the range to 10-20.

This adaptable approach allows for generating random numbers within virtually any desired range. You simply adjust the modulo operand and the added constant to fit your specific requirements. Mastering this technique empowers you to create dynamic and versatile bash scripts for diverse applications.

For instance, simulating dice rolls requires random numbers between 1 and 6. This can be achieved with $(( (RANDOM % 6) + 1 )). This simple yet powerful approach provides the foundation for more complex random number generation tasks in bash.

Generating Random Floating-Point Numbers

While $RANDOM provides integers, generating floating-point numbers requires a different strategy. The shuf command combined with awk offers a solution. shuf -i 0-100 -n 1 | awk '{print $1/100}' generates a random floating-point number between 0 and 1 with two decimal places.

This command uses shuf to generate a random integer between 0 and 100, then divides it by 100 using awk to create the decimal. Adjusting the range in shuf and the divisor in awk allows for controlling the precision and range of the generated floating-point numbers.

This technique can be combined with other bash commands and arithmetic operations to generate floating-point numbers within custom ranges. The versatility of bash allows for creating sophisticated scripts that utilize both integer and floating-point random numbers.

Generating Random Strings

Creating random strings, often used for generating passwords or unique identifiers, involves a different approach. Combining /dev/urandom, tr, and head provides a robust solution. The following command generates a random alphanumeric string of length 10:

tr -dc A-Za-z0-9 </dev/urandom | head -c 10

This command reads from the /dev/urandom device, a source of high-quality random data. tr filters the output to include only alphanumeric characters, and head limits the length of the resulting string. Modifying the character set in tr and the length in head allows for generating strings with specific character sets and lengths.

This method offers a secure and efficient way to generate random strings for various applications. By customizing the character set and length, you can create random strings tailored to your specific needs, whether it be for generating passwords, filenames, or other unique identifiers.

  • Use $RANDOM for basic integer generation.
  • Combine $RANDOM with modulo operator for custom integer ranges.
  1. Determine your desired range.
  2. Apply the appropriate formula using $RANDOM and modulo.
  3. Test your script to ensure accurate random number generation.

Featured Snippet: To quickly generate a random integer between 1 and 100 in bash, use the command $(( (RANDOM % 100) + 1 )). This is the most common method for generating random numbers within a specified range in bash scripts.

Learn more about Bash ScriptingExternal Resources:

[Infographic Placeholder]

Frequently Asked Questions

What is the maximum value of $RANDOM?

The maximum value of $RANDOM is 32767.

How can I generate truly random numbers in bash?

For cryptographic purposes, /dev/urandom provides a source of high-quality random data.

Mastering the art of random number generation in bash scripting opens up a world of possibilities for creating dynamic and versatile scripts. From simulations to data generation and testing, understanding these techniques empowers you to tackle a wide array of scripting tasks effectively. By exploring and implementing the methods outlined in this article, you can enhance your bash scripting skills and build more robust and efficient scripts. Now, experiment with these techniques and discover how random number generation can elevate your bash scripting prowess. Consider exploring further topics such as secure random number generation for cryptography and using random numbers in statistical analysis within bash scripts. The possibilities are limitless!

Question & Answer :
I need to generate a random port number between 2000-65000 from a shell script. The problem is $RANDOM is a 15-bit number, so I’m stuck!

PORT=$(($RANDOM%63000+2001)) would work nicely if it wasn’t for the size limitation.

Does anyone have an example of how I can do this, maybe by extracting something from /dev/urandom and getting it within a range?

shuf -i 2000-65000 -n 1 

Enjoy!

Edit: The range is inclusive.