The tutorial explains the specificities of the Excel random number generator algorithm and demonstrates how to use RAND and RANDBETWEEN functions to generate random numbers, dates, passwords and other text strings in Excel.
Before we delve into different techniques of generating random numbers in Excel, let's define what they actually are. In plain English, random data is a series of numbers, letters or other symbols that lacks any pattern.
Randomness has a variety of different applications in cryptography, statistics, lottery, gambling, and many other fields. And because it has always been in demand, various methods of creating random numbers have existed since ancient times, such as flipping coins, rolling dice, shuffling playing cards, and so on. Of course, we won't rely on such "exotic" techniques in this tutorial and focus on what Excel random number generator has to offer.
Although the Excel random generator passes all standard tests of randomness, it does not generate true random numbers. But don't write it off immediately :) Pseudo-random numbers produced by the Excel random functions are fine for many purposes.
Let's take a closer look at the Excel random generator algorithm so that you know what you can expect from it, and what you cannot.
Like most computer programs, Excel random number generator produces pseudo-random numbers by using some mathematical formulas. What it means for you is that, in theory, random numbers generated by Excel are predictable, provided that someone knows all the details of the generator's algorithm. This is the reason why it has never been documented and will hardly ever be. Well, what do we know about the random number generator in Excel?
If you are looking for true random data, you can probably use a third-party random number generator such as www.random.org whose randomness comes from atmospheric noise. They offer free services to generate random numbers, games and lotteries, color codes, random names, passwords, alphanumeric strings, and other random data.
Okay, this quite lengthy technical introduction ends and we are getting to practical and more useful things.
The RAND function in Excel is one of the two functions specially designed for generating random numbers. It returns a random decimal number (real number) between 0 and 1.
RAND() is a volatile function, meaning that a new random number is generated every time the worksheet is calculated. And this happens every time you perform any action on a worksheet, for example update a formula (not necessarily the RAND formula, just any other formula on a sheet), edit a cell or enter new data.
The RAND function is available in all versions of Excel 365 - 2000.
Since the Excel RAND function has no arguments, you simply enter
=RAND() in a cell and then copy the formula into as many cells as you want:
And now, let's take a step further and write a few RAND formulas to generate random numbers according to your conditions.
To generate random numbers between zero and any N value, you multiple the RAND function by N:
For example, to create a sequence of random numbers greater than or equal to 0 but less than 50, use the following formula:
Note. The upper bound value is never included in the returned random sequence. For instance, if you want to get random numbers between 0 and 10, including 10, the right formula is
To create a random number between any two numbers that you specify, use the following RAND formula:
Where A is the lower bound value (the smallest number) and B is the upper bound value (the largest number).
For example, to generate random numbers between 10 and 50, you can use the following formula:
Note. This random formula will never return a number equal to the largest number of the specified range (B value).
To make the Excel RAND function produce random integers, take either of the above mentioned formulas and wrap it in the INT function.
To create random integers between 0 and 50:
To generate random integers between 10 and 50:
RANDBETWEEN is another function provided by Excel for generating random numbers. It returns random integers in the range that you specify:
Obviously, bottom is the lowest number and top is the highest number in the range of random numbers you want to get.
Like RAND, Excel's RANDBETWEEN is a volatile function and it returns a new random integer every time your spreadsheet recalculates.
For instance, to generate random integers between 10 and 50 (including 10 and 50), use the following RANDBETWEEN formula:
The RANDBETWEEN function in Excel can create both positive and negative numbers. For example, to get a list of random integers from -10 to 10, enter the following formula in your worksheet:
The RANDBETWEEN function is available in Excel 365 - Excel 2007. In earlier versions, you can use the RAND formula demonstrated in Example 3 above.
Further on in this tutorial, you will find a few more formula examples demonstrating how to use the RANDBETWEEN function to generate random values other than integers.
Tip. In Excel 365 and Excel 2021, you can use the dynamic array RANDARRAY function to returns an array of random numbers between any two numbers that you specify.
Though the RANDBEETWEEN function in Excel was designed to return random integers, you can force it to return random decimal numbers with as many decimal places as you want.
For instance, to get a list of numbers with one decimal place, you multiply the bottom and top values by 10, and then divide the returned value by 10:
The following RANDBETWEEN formula returns random decimal numbers between 1 and 50:
In a similar manner, to generate random numbers between 1 and 50 with 2 decimal places, you multiply the RANDBETWEEN function's arguments by 100, and then divide the result by 100 as well:
=RANDBETWEEN(1*100, 50*100) / 100
To return a list of random dates between the given two dates, use the RANDBETWEEN function in combination with DATEVALUE:
For example, to get the list of dates between 1-Jun-2015 and 30-June-2015 inclusive, enter the following formula in your worksheet:
Alternatively, you can use the DATE function instead of DATEVALUE:
Remember to apply the date format to the cell(s) and you will get a list of random dates similar to this:
For a number of advanced options such as generating random weekdays or weekends, check out Advanced Random Generator for dates.
Remembering that in the internal Excel system times are stored as decimals, you can use the standard Excel RAND function to insert random real numbers, and then simply apply the time format to the cells:
To return random times according to your criteria, more specific random formulas are required, as demonstrated below.
To insert random times between any two times that you specify, use either the TIME or TIMEVALUE function in conjunction with Excel RAND:
For example, to insert a random time between 6:00 AM and 5:30 PM, you can use either of the following formulas:
=TIME(6,0,0) + RAND() * (TIME(17,30,0) - TIME(6,0,0))
=TIMEVALUE("6:00 AM") + RAND() * (TIMEVALUE("5:30 PM") - TIMEVALUE("6:00 AM"))
To create a list of random dates and times, use a combinations of RANDBETWEEN and DATEVALUE functions:
Supposing you want to insert random dates between June 1, 2015 and June 30, 2015 with a time between 7:30 AM and 6:00 PM, the following formula will work a treat:
=RANDBETWEEN(DATEVALUE("1-Jun-2015"), DATEVALUE("30-Jun-2015")) + RANDBETWEEN(TIMEVALUE("7:30 AM") * 10000, TIMEVALUE("6:00 PM") * 10000) / 10000
You can also supply dates and times using the DATE and TIME functions, respectively:
=RANDBETWEEN(DATE(2015,6,1), DATE(2015,6,30)) + RANDBETWEEN(TIME(7,30,0) * 10000, TIME(18,0,0) * 10000) / 10000
To return a random letter, a combination of three different functions is required:
Where A is the first character and the Z is the last character in the range of letters you want to include (in alphabetical order).
In the above formula:
Note. Since, the ANSI codes are different for UPPERCASE and lowercase characters, this formula is case-sensitive.
If someone remembers the ANSI Character Codes Chart by heart, nothing prevents you from supplying the codes directly to the RANDBETWEEN function.
For example, to get random UPPERCASE letters between A (ANSI code 65) and Z (ANSI code 90), you write:
To generate lowercase letters from a (ANSI code 97) to z (ANSI code 122), you use the following formula:
To insert a random special character, such as ! " # $ % & ' ( ) * + , - . /, use the RANDBETWEEN function with the bottom parameter set to 33 (ANSI code for "!') and the top parameter set to 47 (ANSI code for "/").
To create a random text string in Excel, you just have to concatenate several CHAR / RANDBEETWEEN functions.
For example, to generate a list of passwords consisting of 4 characters, you can use a formula similar to this:
=RANDBETWEEN(0,9) & CHAR(RANDBETWEEN(65,90)) & CHAR(RANDBETWEEN(97, 122)) & CHAR(RANDBETWEEN(33,47))
To make the formula more compact, I supplied the ANSI codes directly in the formula. The four functions return the following random values:
RANDBETWEEN(0,9)- returns random numbers between 0 and 9.
CHAR(RANDBETWEEN(65,90))- returns random UPPERCASE letters between A and Z.
CHAR(RANDBETWEEN(97, 122))- returns random lowercase letters between a and z.
CHAR(RANDBETWEEN(33,47))- returns random special characters.
The text strings generated with the above formula would be something like "4Np#" or "2Yu&".
A word of caution! If you use a similar formula to create random passwords, they won't be strong. Of course, there's nothing saying you can't generate longer text strings by chaining more CHAR / RANDBETWEEN functions. However, it's impossible to randomize the order or characters, i.e. the 1st function always returns a number, the 2nd function returns an uppercase letter and so on.
If you are looking for an advanced random password generator in Excel capable of producing text strings of any length and pattern, you may want to check out the capabilities of Advanced Random Generator for test strings.
Also, please keep in mind that the text strings generated with the above formula will change every time your worksheet recalculates. To ensure that your strings or passwords remain the same once they are created, you will have to stop the RANDBETWEEN function from updating the values, which leads us directly to the next section.
If you want to get a permanent set of random numbers, dates or text strings that won't change every time the sheet is recalculated, use one of the following techniques:
To learn more about this technique to "freeze" random numbers, see How to replace formulas with values.
Neither of Excel's random functions can produce unique random values. If you want to create a list of random numbers without duplicates, perform these steps:
More solutions can be found in this tutorial: How to generate random numbers without duplicates.
Now that you know how to use random functions in Excel, let me demonstrate you a faster, easier and formula-free way to create a list of random numbers, dates or text strings in your worksheets.
AbleBits Random Generator for Excel was designed as a more powerful and user-friendly alternative to Excel's RAND and RANDBETWEEN functions. It works with all versions of Microsoft Excel 2019, 2016, 2013, 2010, 2007 and 2003 equally well and addresses most of the quality and usability issues of the standard random functions.
Before showing our Random Generator in action, let me provide a few key notes on its algorithm so that you know exactly what we are offering.
Unlike Excel random functions, our Random Number Generator creates permanent random values that do not change when a spreadsheet recalculates.
As already noted, this advanced Random Number Generator for Excel offers a formula free (and consequently error-free :) way to create various random values such as:
And now, let's see the Random Number Generator in action, as promised.
With AbleBits Random Number Generator, creating a list of random numbers is as easy as clicking the Generate button.
Generating unique random integers
All you have to do is select the range to be populated with random integers, set the bottom and top values and, optionally, check the Unique Values box.
Generating random real numbers (decimals)
In a similar manner, you can generate a series of random decimal numbers in the range that you specify.
For dates, our Random Number Generator provides the following options:
Apart from random numbers and dates, with this Random Generator you can easily create random alphanumeric strings with certain character sets. The maximum string length is 99 characters, which allows generating really strong passwords.
A unique option provided by AbleBits Random Number Generator is creating random text strings by mask. This is a very useful feature for generating globally unique identifiers (GUID), zip codes, SKUs, and so on.
For example, to get a list of random GUIDs, you select the Hexadecimal character set and type ????????-????-????-???????????? in the Mask box, as demonstrated in the screenshot:
If you are interested to give our Random Generator a try, you are most welcome to download it below as part of our Ultimate Suite for Excel.
Table of contents