How to Generate Random Unique Numbers in Excel

Struggling to generate random unique numbers in Excel?

Common methods often lead to duplicates, frustrating many users. However, Excel has built-in functions like RANDBETWEEN and COUNTIF that solve this problem when used together. This combination creates a dynamic list of unique numbers, refreshing with every spreadsheet update.

By the end of this read, you’ll master generating a unique, random number list in Excel – a skill that enhances data analysis and decision-making.

Understanding Excel’s Randomization Functions

To generate unique random numbers in Excel, it’s essential to familiarize yourself with three core functions: RAND, RANDBETWEEN, and RANDARRAY. Each serves a unique purpose and can be applied to different scenarios to assist in producing random values.

Leveraging RAND Function

The RAND function is your go-to solution for generating random decimal values between 0 and 1. It’s a volatile function that recalculates every time the worksheet recalculates. To apply this function, you would simply use:


It requires no arguments, and whenever you need a new set of values, just hit recalculate (F9 shortcut key).

Using RANDBETWEEN Function

For times when you need random whole numbers within a specific range, use the RANDBETWEEN function. This formula requires two arguments:

  • lower_limit: The smallest integer you want to generate.
  • upper_limit: The largest integer you want to generate.

The syntax would look like this:

=RANDBETWEEN([lower_limit], [upper_limit])

Like RAND, this is also a volatile function and will produce new numbers with each recalculation.


Exploring RANDARRAY Function

For users of Microsoft 365 with access to dynamic arrays, RANDARRAY is the dynamic array function you’ll want to utilize for generating multiple random values simultaneously. It can create both decimal and whole numbers and offers several arguments for customization:

  • rows: The number of rows of random numbers you require.
  • columns: The number of columns of random numbers you require.
  • min: The minimum value for the random numbers (inclusive).
  • max: The maximum value for the random numbers (inclusive).

Here’s a basic use of the RANDARRAY function:

=RANDARRAY([rows], [columns], [min], [max])

The RANDARRAY function outputs a spill range, automatically filling adjacent cells with values and eliminating duplicates within its output for unique random numbers.


Generate Random Unique Numbers

To generate a list of unique random numbers in Excel, you must combine functions or use Excel’s dynamic array capabilities.

Method 1 – Sort Random numbers

When you’re using a version of Excel that doesn’t support dynamic arrays (anything before Excel 365), you can combine the RAND function with a few additional steps to create unique random numbers. This is quite a manual approach and would be best used for one-off situations.

First, generate an array of random decimal numbers by filling a column with the RAND() function. In a second column, input a series of sequential integers as your unique identifiers. Here’s a brief walkthrough:

  1. In the first column (let’s say A2), use =RAND().
  2. In the second column (B2), insert sequential numbers (1, 2, 3, …).
  3. Copy and paste the resulting numbers as values to prevent them from recalculating.
  4. Select both columns and use the Sort feature to sort by the first column.

By sorting the random decimal numbers, the accompanying integers will rearrange into a unique, random sequence.

RAND sort method

Method 2 – Use a Helper Column

Another more dynamic approach is creating a helper column with RANK and COUNTIF.

Here are the steps:

  1. Use the RAND or RANDBETWEEN function in column A.
  2. Set up a helper column in column B with the formula =RANK.EQ(A2, $A$2:$A$10) + COUNTIF($A$2:A2, A2) – 1. This formula ranks each number and adjusts for duplicates by counting and adding to the rank value, depending on how many times the number has appeared up to that row. See the example below.
  3. Use the Remove Duplicates function to leave only unique values in column A if needed.

As mentioned, the RAND and RANDBETWEEN functions recalculate when the worksheet is opened or changed. Press F9 to refresh and generate new random numbers. If you want to keep the generated numbers static and not change every time the sheet recalculates, copy the cells and paste them as values.

RANDBETWEEN with a helper column

Method 3 – Dynamic Array and SORTBY Function

For those with Excel 365, unique random number generation becomes much simpler thanks to dynamic array functions.

With the RANDARRAY function, you can create an array of unique random decimal values and pair it with the SORTBY function to sort another range of unique identifiers. Here’s how to do it in a couple of steps:

  1. Generate your array of unique sequential identifiers (e.g., 1, 2, 3, …).
  2. Use =RANDARRAY(count_of_identifiers) to create a matching array of random decimals.
  3. Apply the =SORTBY(sequence, RANDARRAY) function to sort the sequence by the random array.

This will yield a sorted list of unique random numbers without duplicates. The RANDARRAY function creates an array of random values, and the SORTBY function sorts the sequence based on these values, giving you a list of unique random numbers with no repeats.

Troubleshooting Tips

Here are a few quick ways to help identify unique values on your spreadsheet.

  • Consider using the Remove Duplicates option under the Data tab if you prefer a manual approach or a double-check of your data.
  • To avoid duplicates when using RANDBETWEEN(), pair it with the COUNTIF function to check if the generated number already exists in your dataset—this results in additional formulas but is a robust way to prevent duplicates.
  • Set up a conditional formatting rule to highlight any duplicates that might occur so you can easily spot and remove them.

Advanced Techniques and Applications

Implementing VBA for Custom Randomization

Using VBA in Microsoft Excel, you can create a highly customizable random number generator that meets complex criteria. VBA can be accessed through Excel’s Developer tab.

To generate unique random numbers, use the following VBA function code:

Function GenerateUniqueRandoms(lngMin As Long, lngMax As Long, lngQty As Long) As Variant
    Dim Numbers() As Long
    Dim i As Long
    Dim lngRand As Long
    Dim blnExists As Boolean

    ReDim Numbers(1 To lngQty)
    For i = 1 To lngQty
            lngRand = Int((lngMax - lngMin + 1) * Rnd + lngMin)
            blnExists = Not IsError(Application.Match(lngRand, Numbers, 0))
        Loop While blnExists
        Numbers(i) = lngRand
    Next i

    GenerateUniqueRandoms = Numbers
End Function

Customize your VBA code to accommodate other requirements, like generating numbers for different groups or keeping track of the maximum values generated.

Random number UDF VBA Excel

Frequently Asked Questions

Is there a formula in Excel to ensure random numbers produced are always unique?

Excel does not have a single formula that guarantees unique random numbers. However, you can combine multiple functions to create such a system.

For example, use RANDBETWEEN to generate random numbers and combine them with a helper column using COUNTIF to check for duplicates.

What’s the process of generating a sequence of random numbers from a fixed list?

You can use the INDEX, RAND, and RANK functions to generate a random sequence from a fixed list in Excel.

First, assign a random number to each item in your list using RAND. Then, use the RANK.EQ function to rank these random values in another column. Finally, use the INDEX function to create the random sequence.

This method ensures that each item from your original list appears once in the random sequence.

How can I ensure my random numbers remain constant and don’t change with each spreadsheet recalculation?

To keep random numbers constant in Excel, you must convert them from formulas to static values. This can be done by copying the cells with the random numbers and using ‘Paste Special’ to paste them as values.

This process removes the formula, thus preventing the numbers from changing on recalculation.

Leave a Comment