Excel Random Number Not Repeating
In this article
You can generate random numbers without repeats in Excel using the RANDBETWEEN function combined with the INDEX, MATCH, and COUNTIF functions, or by using the Dynamic Array formula =RANDARRAY and UNIQUE. The most reliable method for older Excel versions is the INDEX+MATCH+COUNTIF combination, while newer versions can use the simpler RANDARRAY and UNIQUE functions.
What You'll Need
- A computer with Microsoft Excel installed (2019, 2021, or Microsoft 365 are recommended).
- A spreadsheet containing the data you want to select from.
- Basic familiarity with spreadsheet formulas, specifically referencing cells and ranges.
Step-by-Step
- Open your Excel workbook. Locate the sheet where you want to generate the unique random numbers. It is best to keep this sheet separate from the data you are selecting from to avoid confusion, or use a helper column if you must work on the same sheet.
- Determine the range of your data. Identify the specific cells that contain the list of items, names, or numbers you intend to assign a random value to. For example, if your list is in cells A1 through A10, your range is A1:A10.
- Select the first cell for your result. Click on the cell where you want the first random number to appear. This will be the top-left cell of the block where your unique random numbers will be generated.
- Enter the formula for older Excel versions (2019 or earlier). Type the following formula into the selected cell:
=INDEX($A$1:$A$10, MATCH(0, COUNTIF($B$1:B1, $A$1:$A$10), 0))Replace$A$1:$A$10with the actual range of your data. The dollar signs ($) are critical; they lock the data range so it does not move when you copy the formula down. The range$B$1:B1is a dynamic range that expands as you copy the formula, tracking which items have already been selected. - Verify the formula syntax. Ensure that all parentheses are closed and that the cell references match your data. A common error is missing the dollar signs on the data range, which will cause the range to shift and return incorrect results or errors.
- Press Enter. The first random number from your list should appear in the selected cell. The value will be one of the items or numbers from your data range.
- Copy the formula down. Click on the bottom-right corner of the cell containing the formula (the fill handle) and drag it down for as many cells as you need unique random numbers. If you need 5 unique numbers, drag down 5 rows. Excel will automatically expand the helper range
$B$1:B1to$B$1:B2,$B$1:B3, and so on, preventing duplicates. - Verify the results. Check the generated list to ensure all values are unique and are present in your original data range. If you see a duplicate, double-check that the dollar signs were applied correctly to the data range in the formula.
- For newer Excel versions (Microsoft 365 or Excel 2021), use the Dynamic Array formula. If you are using a modern version of Excel, you can use a much simpler two-step process. In the first cell, enter the formula:
=RANDARRAY(10, 1, 1, 100, TRUE)This formula generates 10 random integers between 1 and 100, sorted in ascending order, and ensures no duplicates. Adjust the 10, 1, 1, and 100 to match your needs. - Apply the SORT and UNIQUE functions. In the cell directly below the RANDARRAY result, enter:
=SORT(UNIQUE(RANDARRAY(10, 1, 1, 100, TRUE)))This combines the functions to first generate unique random numbers, then sort them. You can also omit the SORT function if you prefer the unsorted list. This method is far less prone to errors than the INDEX/MATCH/COUNTIF method.
Common Mistakes
The most frequent mistake users make when trying to generate unique random numbers is failing to lock the data range with dollar signs. If you omit the $ symbol before the row and column numbers in your reference (e.g., using A1:A10 instead of $A$1:$A$10), the range will shift as you copy the formula down. This causes the formula to look at the wrong cells, leading to circular references or errors like #N/A. Always double-check that the first and last cells in your range are locked with dollar signs.
Another common error is not expanding the COUNTIF range correctly. The formula relies on the range $B$1:B1 to grow as you copy it down. If you manually enter a fixed range like $B$1:B10 in every cell, the formula will eventually count the same item multiple times, resulting in duplicates. Dragging the fill handle is the only way to ensure this range expands automatically.
Users often try to use the RANDBETWEEN function alone to generate a list of unique numbers. While this function can generate a single random number, it has no built-in logic to prevent repeats. It will simply pick a random number from the specified range every time it is recalculated, making duplicates a certainty over a small list. The unique logic must be added using the INDEX, MATCH, and COUNTIF functions.
Tips
When using the INDEX/MATCH/COUNTIF method, it is good practice to select the entire block of cells where you want the results to appear before entering the formula. This ensures that the formula is applied to all cells at once, and Excel will automatically adjust the relative references (like the helper range) for each row. This saves time and reduces the risk of manual copy-paste errors.
For very large datasets (over 1,000 rows), the INDEX/MATCH/COUNTIF formula can become slow to recalculate. This is because the COUNTIF function must scan the entire list of already-selected items for every single cell. If you are working with a large dataset, consider using a helper column to manually track which items have been selected, or switch to the SORT(UNIQUE(RANDARRAY(...))) method if your Excel version supports it.
Always remember that these formulas are volatile. This means that the random numbers will change every time you press F9 to recalculate the sheet or make any change to any cell in the workbook. Once you have the list in the state you want it, you should copy the cells and paste them back as values to lock them in place. You can do this by selecting the cells, pressing Ctrl+C, then right-clicking and selecting "Paste Values" from the context menu.
Troubleshooting
If you are seeing #N/A errors in your random number list, it usually means that you have requested more unique numbers than there are items in your data range. For example, if your data range has 5 items, you cannot generate 6 unique random numbers. The formula has run out of items to select from. Reduce the number of cells you have copied the formula down to match the number of items in your data range.
If the formula returns duplicate numbers, the most likely cause is that the dollar signs were omitted from the data range reference. As mentioned in the common mistakes section, this causes the range to shift when copying the formula, leading to incorrect calculations. Double-check the formula in the first cell and ensure that the range for your data is locked with dollar signs.
If the results seem random but do not match your data range, check that the range in the formula exactly matches the cells containing your data. A common error is referencing the wrong column or including empty cells in the range. You can use the "Go To Special" feature (Home > Find & Select > Go To Special) to select only the cells with values in your range to ensure you are referencing the correct set of data.
If the formulas are not updating when you expect them to, check your Excel calculation settings. By default, Excel is set to "Automatic" calculation, but it can be changed to "Manual" if the workbook is large and slow. You can check this by going to the Formulas tab and looking at the Calculation Options group. If it is set to "Manual", click the "Automatic" button to force the sheet to recalculate.
Frequently Asked Questions
Why does my random number list keep changing?
Excel's random functions, such as RANDBETWEEN and RAND, are volatile. This means they recalculate their values every time you make a change to the sheet or press F9. To stop the numbers from changing, copy the cells and paste them back as values using "Paste Values".
Can I generate random numbers from a specific list, like names?
Yes. The INDEX function allows you to pull a value from a specific range based on the result of the MATCH function. Simply replace the numbers in your data range with the names in your list, and the formula will generate random names instead of random numbers.
Is there a limit to how many unique numbers I can generate?
There is no hard limit imposed by Excel on the number of unique numbers you can generate. However, the performance of the formula will degrade as the list gets larger. For very large lists, expect the calculation to take longer and the sheet to become less responsive.
How do I generate random numbers sorted in ascending order?
If you are using the INDEX/MATCH/COUNTIF method, the results will not be sorted. If you need them sorted, you can use the SORT function on the results. In newer versions of Excel using the RANDARRAY method, you can include the SORT function directly in the formula as shown in the step-by-step guide.
What is the difference between RAND and RANDBETWEEN?
The RAND function generates a random decimal number between 0 and 1. The RANDBETWEEN function generates a random integer between two specified numbers that you define. For generating unique random numbers from a list, the INDEX/MATCH/COUNTIF method is typically used, which relies on the MATCH function, not RANDBETWEEN.