Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, and 2016. 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

by Allen Wyatt
(last updated March 26, 2016)

5

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 couple of different worksheet functions you can use to pick items from the list, such as RANDOM and RANDBETWEEN.

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.

One way to work around this potential problem 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. 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. 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 is because the chance that RAND will return two identical values is infinitesimally small, whereas the chances of RANDBETWEEN 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, and 2016. 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

Reducing File Size

As you work with a workbook (particularly one that contains macros) you may notice that the workbook size can become ...

Discover More

Always Open at 100% Zoom

Tired of shared workbooks opening at some strange zoom factor that makes viewing your data difficult? Here's how to make ...

Discover More

Fixed-Width Settings when Converting Text to Columns

The Convert Text to Columns capabilities of Excel are very helpful when pulling apart information. When working with ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (ribbon)

Segregating Numbers According to Their Sign

Remember your number line from your early years in school? Some numbers can be below zero (negative numbers) and others ...

Discover More

How Operators are Evaluated

Operators are used in formulas to instruct Excel what to do to arrive at a result. Not all operators are evaluated in the ...

Discover More

Totaling Across Worksheets

Want to sum the values in the same cell on a range of worksheets? It's not as easy as summing a range on the same ...

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}] 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 + 2?

2016-03-28 12:53:13

Michael (Micky) Avidan

@Pieter de la Courtת
Take a look and check Marcel Beugelsdijk's suggestion.
--------------------------
Michael (Micky) Avidan
“Microsoft® Answers" - Wiki author & Forums Moderator
“Microsoft®” MVP – Excel (2009-2016)
ISRAEL


2016-03-28 06:39:03

Pieter de la Court

@Michael
You are right that this becomes very complicated for more than two random items from the list.
I don't understand your other remarks about sequential order and exceeding total items count. I see no sequence, no error. The principle of my formula is that you eliminate the risk of picking the same item twice, by skipping the first selection in the second selection process.
I look forward to your solution for larger numbers of random items.


2016-03-27 23:16:25

Marcel Beugelsdijk

Alternatively you can pick from the remaining entries with the following array formula in C2 and down:
=INDEX($A:$A,SMALL(IF(ISNA(MATCH(Items,C$1:C1,0)),ROW(Items)),RANDBETWEEN(1,COUNTA(Items)-ROWS(C$2:C2)+1)))


2016-03-27 08:25:36

Michael (Micky) Avidan

@Pieter de la Court,
I assume that your reply/suggestion refers to a VERY Private case where only TWO Names are going to be retrieved from range A2:A26.
If not - I would be happy to see your suggested formula for retrieving 12 UNIQUE Names.
To my opinion your formula will retrieve the names in their sequential order and will fall on an Error if the indexed number will exceed the total items count.
There is, of course, a not too long formula for retrieving 12 unique items but I will present it after your reply.
--------------------------
Michael (Micky) Avidan
“Microsoft® Answers" - Wiki author & Forums Moderator
“Microsoft®” MVP – Excel (2009-2016)
ISRAEL


2016-03-26 09:28:56

Pieter de la Court

There is a more rigourous and yet simle way to avoid getting two the same rows using the Randbetween() funtion: take the second Randbetween function in of one row less, i.e formula in C3 is =Randbetween(1,Rows(Items)-1). Then use an IF function (in a separate cell, e.g. C4) to increment the result in order to skip the row already used: =IF(C3>=C2, C3+1,C3). C4 now gives the index to the second selected item, with zero probability of having the same item twice, while avoiding the calculation of a whole column of random numbers.


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.