Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Picking Different Random Numbers from a Range.
Written by Allen Wyatt (last updated June 8, 2022)
This tip applies to Excel 97, 2000, 2002, and 2003
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 (2874) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Picking Different Random Numbers from a Range.
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!
Normally you use the RAND and RANDBETWEEN functions to generate random numbers. What if you want to generate random ...
Discover MoreExcel provides several different functions that you can use to generate random numbers. One of the most useful is the ...
Discover MoreRandom values are often needed when working with certain types of data. When you need to generate a random value in a ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)
Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments