Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Picking Different Random Numbers from a Range.

Picking Different Random Numbers from a Range

Written by Allen Wyatt (last updated May 28, 2022)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021


1

Let's say that you have a list of employees (or products, or widgets, or whatever) and that you want to pick two items from this list, at random. There are a few of different worksheet functions you can use to pick items from the list, such as RAND, RANDBETWEEN, and RANDARRAY.

Many people use the RANDBETWEEN function because it is very easy and provides a random number within a range. Thus, if you have 25 items in your list, RANDBETWEEN can return a number between 1 and 25, which can then correspond to items in the list.

For example, let's say that your list of items is in the range A2:A26, and that you give this range the name Items. You could then put the following formula in cell C2 to return a number between 1 and the number of Items:

=RANDBETWEEN(1,ROWS(Items))

Copy this formula to cell C3, and you now have two random numbers that represent items from the list. In cell D2 and D3 you could put formulas like this to get the actual names from the list:

=INDEX(Items,C2)

The only problem with this approach is that it is possible for both instances of RANDBETWEEN (cells C2 and C3) to return the same value, and thus you end up with the same item selected twice from your list.

An approach that uses RANDARRAY (which is available only in the Office 365 version of Excel) is subject to the same issue. In cell C2 you could use the following formula:

=RANDARRAY(2,1,1,ROWS(Items),TRUE)

The results of the formula spill into cell C3, and you end up with two random numbers that represent items from the list. Again, these returned values could be the same as each other.

One way to work around this potential problem with RANDBETWEEN and RANDARRAY is to actually select three items from the list instead of two. If the first two items are the same, then the third can be used as a "fallback" item to provide the unique second. (To accomplish this with RANDARRAY, just change the first parameter from 2 to 3.) The method is not foolproof, as it is possible—but quite improbable—that all three will be the same.

A different approach to selecting items from the list would be to assign each item its own random value, and then select based on the highest number in the series. The RANDOM function returns a random value between 0 and 1. In each cell of column B, just to the right of each item in column A, put this formula:

=RAND()

Select the range of cells (B2:B26) and name the range, using a name such as ItemNums. You can then determine the first random name from the list by using the following array formula:

=OFFSET(A$1,SUM((LARGE(ItemNums,1)=(ItemNums))*ROW(ItemNums))-1,0)

In order to signify that this is an array formula, enter it by pressing Shift+Ctrl+Enter. (This is necessary only in versions of Excel before the version with Office 365. In the Office 365 version, just press Enter.) The formula should return a single name. You can then use the following array formula to return the second name:

=OFFSET(A$1,SUM((LARGE(ItemNums,2)=(ItemNums))*ROW(ItemNums))-1,0)

The reason that using the RAND function approach works better than using RANDBETWEEN and RANDARRAY is because the chance that RAND will return two identical values is infinitesimally small, whereas the chances of RANDBETWEEN and RANDARRAY doing so is much higher.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12082) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. You can find a version of this tip for the older menu interface of Excel here: Picking Different Random Numbers from a Range.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Understanding the While...Wend Structure

Logical structures are important in programming, as they allow you to control how the programming statements are ...

Discover More

Changing Text Orientation

Word allows you to change the orientation of text contained within certain objects, such as shapes, text boxes, and table ...

Discover More

Changing Paragraph Order

Want a quick way to rearrange entire paragraphs of your document? You can easily do it by using the techniques described ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More ExcelTips (ribbon)

Indirectly Referencing a Cell on a Different Worksheet

Excel includes the powerful INDIRECT function which can be used to assemble references to other cells in your workbook. ...

Discover More

Using a Numeric Portion of a Cell in a Formula

If you have a mixture of numbers and letters in a cell, you may be looking for a way to access and use the numeric ...

Discover More

Generating a Gift Exchange List

Want to figure out how to do a gift exchange for your family or office? There are a variety of ways you can approach the ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 6 - 6?

2022-05-30 05:00:49

Dave S

You could perhaps reduce the infinitesimally small chance of a duplicate second item even further by using something like

=OFFSET(A$1,SUM((SMALL(ItemNums,1)=(ItemNums))*ROW(ItemNums))-1,0)


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.