How to Generate Non-Repeating Random Numbers in Excel? Shuffle First, Then Pick
Using RANDBETWEEN directly produces repeated numbers. This article explains how to generate random integers within a range that have no duplicates in Excel, multi-digit numbers whose digits are all different, and numbers whose last digit is even, and provides copyable formulas and verification methods.
Key Points of This Article
- Ordinary random functions draw independently each time, so duplicates can appear; generate the complete candidate sequence first, then shuffle it and take the first few, which guarantees no repetition.
- To get non-repeating integers within an interval, use SEQUENCE to generate the candidate numbers, combine with RANDARRAY to shuffle them, then use INDEX to take the first N.
- For multi-digit numbers where each digit must be unique, randomly arrange the digits from 0 to 9, take the first few digits and concatenate them, and check whether the leading digit is allowed to be 0.
- When the last digit must be even, first choose the last digit from 0, 2, 4, 6, 8, then choose the middle digits from the remaining digits, to avoid repeated digits.
- After generating, verify with COUNTIF and digit checks; random results change with the worksheet, so once confirmed correct, you can paste as values to fix them.
Many people generate random numbers in Excel with RANDBETWEEN, and after drawing a few dozen numbers, they find that some of them have repeated. For lottery numbers, group IDs, test samples, or random strings used as passwords, any duplicate can cause problems. What you want is "how to generate random numbers in Excel without repetition," not ordinary random sampling.
These kinds of requirements often come with extra conditions: each number in the range must appear only once, every digit in a multi-digit number must be different, or the last digit must be even. The formula structure for these constraints differs from ordinary random numbers, so simply wrapping RANDBETWEEN and modifying it won't work. This article explains the approach and provides formulas you can directly adapt by swapping in your own numbers, so you can follow along in your own spreadsheet.
How to generate non-repeating random numbers in Excel? One-sentence answer
Conclusion: don't have each cell draw its own random number independently. Instead, first list all candidate numbers in a column, then shuffle that list randomly, and finally take the first few. This way the candidates can't repeat each other, so the drawn results are naturally unique. Below, choose the approach that fits your situation.
| Your situation | Recommended Practices | Cause |
|---|---|---|
| Pick several distinct integers from a given range. | Generate all integers in a range with SEQUENCE, shuffle them with SORTBY and RANDARRAY, then use INDEX to take the first N. | The candidate pool is fixed; after shuffling, take the first few, so no duplicate values will be drawn. |
| Generate multi-digit numbers in which every digit is different | Randomly permute 0 to 9, take the first few digits, and join them with TEXTJOIN | There are only ten digits, so a random arrangement naturally ensures that every digit is different. |
| Multi-digit numbers require the last digit to be even. | First choose the last digit from 0, 2, 4, 6, 8, then choose the remaining digits from the leftover numbers. | First fix the last digit, and the following numbers won't collide with it. |
| The Excel version is old and doesn't have newer functions like SORTBY. | In a helper column, generate random numbers with RAND, then use SMALL and MATCH to pull out the sorted results. | The older version can also achieve the same shuffling effect; it just needs a few more columns in the formula. |
Before You Begin: Confirm These Points
Before you start, clarify the following points; otherwise, applying the formula may give incorrect results.
- Excel version. SORTBY, RANDARRAY, SEQUENCE, FILTER, TEXTJOIN and LET require a newer version (generally Excel 365 or 2021). If you're using an older version, the helper-column method in the FAQ at the end is safer.
- The candidate pool must be larger than the number you need. If you want 15 non-repeating numbers from 1 to 10, no shuffle can produce that. Work out how many integers the range contains before deciding how many to draw.
- Whether a leading zero is allowed. A multi-digit number built from the digits 0 to 9 may start with 0. "0371" would become the number 371 in Excel, so the digit count would not match. If a fixed number of digits is required, choose the first digit from 1 to 9.
- Whether the results must stay fixed. A random function in a formula changes every time the sheet recalculates. If the results need to be saved for others, copy them first, then use "Paste Special > Values".
- Whether uniqueness must also hold across rows. Multi-digit numbers generated row by row may coincide between two rows. If no duplicates are allowed across the whole column, run a duplicate check after generation.
Steps to Generate Non-Repeating Random Integers Within a Range in Excel 365
Below, we use the example of "randomly selecting 5 distinct integers from 10 to 50."
- Select the starting cell where the results will go, for example A2.
- Enter the formula
=INDEX(SORTBY(SEQUENCE(41,1,10,1),RANDARRAY(41)),SEQUENCE(5)). SEQUENCE(41,1,10,1) generates the 41 integers from 10 to 50. This is the candidate pool. - Press Enter. The formula automatically spills 5 results downward, and they are all different from each other.
- If the range or the count changes, you only need to change two numbers: the count and starting value in SEQUENCE, and the count in RANDARRAY. Make sure the number of candidates is not less than the number of values to pick.
- Once the results appear, use the method in the next section to check for duplicates.
- For long-term storage, select the results, copy them, then use "Paste Special" as "Values" to overwrite the original formula.
Generate multi-digit numbers with no repeated digits. Enter `=TEXTJOIN("",TRUE,INDEX(SORTBY(SEQUENCE(10,1,0,1),RANDARRAY(10)),SEQUENCE(4)))` in a cell. It randomly arranges the digits 0 to 9 and takes the first 4 to form a text-type number. To generate multiple rows, fill down; each row is randomized independently. This formula allows the first digit to be 0. If you need the first digit to be non-zero, use the formula below.
The last digit is even, the first digit is non-zero, and the four digits are all different. You can use LET to break the steps apart. Enter this in the cell: =LET(l,INDEX({0,2,4,6,8},RANDBETWEEN(1,5)),p,FILTER(SEQUENCE(9,1,1,1),SEQUENCE(9,1,1,1)<>l),f,INDEX(SORTBY(p,RANDARRAY(ROWS(p))),1),r,FILTER(SEQUENCE(10,1,0,1),(SEQUENCE(10,1,0,1)<>l)*(SEQUENCE(10,1,0,1)<>f)),TEXTJOIN("",TRUE,f,INDEX(SORTBY(r,RANDARRAY(ROWS(r))),SEQUENCE(2)),l)). It first chooses the last digit l, then chooses the first digit f from the digits that are non-zero and not equal to l, and finally chooses the middle two digits from the remaining digits. To change the middle part to a different number of digits, adjust the count in SEQUENCE.
Verify item by item after completion
Random results shouldn't be used after just a single glance. Check the following points one by one.
| Inspection item | How do you see it? | What should I do if I have a problem? |
|---|---|---|
| Are the numbers within the interval repeated? | Enter =SUMPRODUCT(--(COUNTIF(A2:A6,A2:A6)>1)) next to it; if the result is 0, it means there are no duplicates. | If it is not 0, check whether the candidate pool uses RANDBETWEEN, or whether it has been changed manually. |
| Do all digits of a multi-digit number differ from one another? | Enter =ROWS(UNIQUE(MID(A2,SEQUENCE(LEN(A2)),1)))=LEN(A2); if the result is TRUE, it means all characters are different. | If the result is FALSE, it's most likely because the concatenation used a repeatable random function. Switch to the random permutation method above. |
| Is the last digit even? | Input =MOD(RIGHT(A2,1),2)=0, the result is TRUE, which means the last digit is even. | If the value is FALSE, check whether the last digit is drawn from the set of even numbers, and confirm that it is not overwritten by another formula. |
| Are the digit count and the leading digit correct? | Check whether the length meets the requirement, and whether the first digit is 0. | If the leading 0 is not allowed, switch to a LET formula whose first digit is not 0, or use text format to keep the digits. |
| Is the result already fixed? | Does the number change after pressing F9? | The change description is still a formula; after confirming it is correct, copy and paste it as values. |
Also note that random numbers are not reliable just because they "look non-repeating." The checks above only confirm the format and the no-duplicate condition; they do not mean the results are statistically random in every sense. If this is used for an official lottery or draw, it is recommended to archive both the generation process and the results so they can be verified afterward.
❓ Frequently Asked Questions (FAQ)
1. Why does RANDBETWEEN generate duplicate numbers?
RANDBETWEEN draws each cell independently, so even with a small range the same number can be drawn more than once. It keeps no record of what was drawn before, so to guarantee no duplicates, you must switch to the method of "building a candidate pool first, then shuffling it."
2. What if my Excel doesn't have SORTBY and RANDARRAY?
You can use a helper column. Enter the candidate numbers in one column, use RAND in the adjacent column to generate random decimals, then use =INDEX(候选列,MATCH(SMALL(随机列,ROW()-1),随机列,0)) in a third column to pull values in random order. This approach also works in older versions, and you can adjust the number of rows returned as needed.
3. The generated numbers change every time it's opened — how do I fix them?
This is normal behavior for random functions. Once you've confirmed the results, select the range, copy it, and then paste over it using "Paste Special > Values." After that, the table won't change when it recalculates.
4. Can the leading digit of a multi-digit number be 0?
It depends on the purpose. If it is just a random number, a leading 0 is allowed and you can simply store it as text; if a fixed number of digits is required or the numeric value matters, the first digit should be chosen from 1 to 9. The LET formula provided above was designed for a non-zero first digit.
5. Will formulas be slow when there are many candidate numbers?
The candidate pool usually consists of dozens to hundreds of numbers, which has very little impact on an ordinary spreadsheet. If you need to draw from a very large range, it is recommended to generate the values in batches, or first take a smaller candidate pool and then draw from it, so that the spreadsheet does not slow down.
📝 Summary
In Exc


