How to Set bash Variables to Random Numbers

You can easily generate a random number at the bash prompt, which you can then use to set said number as a variable. This is useful for everything from writing certain types of scripts to running tabletop roleplaying adventures. Whether you might be writing a D&D campaign or authoring a utility in the powerful bash language, you could usually get away with a single line of code. This is also a very useful tool when attempting to preform certain security checks, but you’ll want to remember that good crackers can circumvent these simple number generators. They’re still useful for generating certain types of passwords and code, which might help to keep things secure anyway.

There are a few different ways to accomplish this, and not every technique is right for every situation, so we did our best to take a look at each way and tested it to ensure it worked. You’ll need to be working from a terminal for this, so either use Ctrl+Alt+T or Super (Windows)+T to open a graphical terminal, start one by typing terminal in the Dash, select it from the Application or Whisker and System Tools menus or head to a virtual terminal in earnest by using Ctrl, Alt and a key between F1 and F6.

Method 1: Picking a Full Random Byte

You can generate a random number from between 1 and 256 by running the following command directly from the bash prompt:

od -An -N1 -tu1 /dev/urandom

While you might sometimes see this command referenced to /dev/random instead of /dev/urandom, you probably won’t want to run this way. This can unintentionally rob other programs from being able to use the random seed generator during your use of it. Once you’ve run the program and gotten a number, you can be sure it will work this way as well:

ranNum=$(($(od -An -N1 -tu1 /dev/urandom)))

This will set the variable ranNum to a number between 1 and 256, selected at the time you run it. You can run it from the command line or from inside of a script and it should work either way. Keep in mind that you can replace ranNum with any valid variable name.

Method 2: Setting a Variable to a Random Number Between 1 and Whatever

You can run echo $[RANDOM%40+1] from the command line to send a random number between 1 and 40 straight to standard output, but you could also replace the 40 in the command with virtually any number. Eventually your host machine’s architecture will run out of digits or perhaps bash will, but it’s doubtful you need such a high number anyway.

Let’s assume you’d like to generate a random number between 1 and 10 for your variable configuration. You could run:


Keep in mind that you can replace 10 with whatever upper value that you want. This value is inclusive, meaning that 10 is a valid returned figure. If you wanted to reduce it by one, then simply make the value 9 instead of using C or C++ style -1 mathematics. These are unnecessary when setting a variable using this method, and they’ll actually generate unexpected results by adding or subtracting digits.

Method 3: Selecting a Single Random Bit

If you need to set a variable to a random bit of either zero or one, then you may wish to modify the previous code as follows:

ranNum=$(($(od -An -N1 -i /dev/urandom) % 2))

This code will set ranNum to either 1 or 0 depending on whether the numbers that it sampled from the /dev/urandom file were even or odd. This can be useful if you need to set a simple yes or no equation to one or the other state.

If this isn’t sufficiently random for what you’re working on, then you might get a slightly more random number by making a call to the /proc/sys/kernel/random directory with:

ranNum=$((0x$(cut -c1-1 /proc/sys/kernel/random/uuid) % 2))

Once again, though, this will only set the variable to 1 or 0, which is perfect for binary logic but not as useful for times when you might need a longer number. You could, however, use any of these lines in a bash script if you had any need to do so. This  second option might actually be better for situations where the first one fails as well

Method 4: Authoring bash Scripts to Return Random Numbers

If you want to have a handy tool for generating a random number at any time, then you can create a command line script to do so.  This uses nothing more than the standard bash language most modern Linux prompts have, so pretty much any modern Linux distribution and quite a few of the BSD-based operating systems should handle this just fine. You may wish to use cd ~/.local/bin before going any further to make sure that you’ll have your script in a place you can execute it from anywhere.

Type nano random or vi random to begin, though you could use a different name for your script if you’d like. You could also use another text editor as well if you’d prefer. Say you wanted to create one that would return either 1 or 0 when typed at the command line. Enter the following and then push Ctrl+O to save it if you’re using nano:

#!/bin/bash ranNum=$(($RANDOM % 2)) echo $ranNum

Type Ctrl+X to exit the text editor and once back at the command line use chmod +x random to make your script usable. You could of course use ranNum=$[RANDOM%10+1],  ranNum=$(($(od -An -N1 -tu1 /dev/urandom))) or any other line in place of  ranNum=$(($RANDOM % 2)) so you could control exactly what sort of number you’d want back. Let’s say you wanted to select a number between 1 and 50 regularly. Change the bash script’s text to:

#!/bin/bash ranNum=$[RANDOM%50+1] echo $ranNum

You can now easily run it from the command line by typing random whenever you’d like to generate a random number.

Kevin Arrows
Kevin is a dynamic and self-motivated information technology professional, with a Thorough knowledge of all facets pertaining to network infrastructure design, implementation and administration. Superior record of delivering simultaneous large-scale mission critical projects on time and under budget.

Expert Tip

How to Set bash Variables to Random Numbers

If the issue is with your Computer or a Laptop you should try using Restoro which can scan the repositories and replace corrupt and missing files. This works in most cases, where the issue is originated due to a system corruption. You can download Restoro by clicking the Download button below.

Download Now

I'm not interested