Generate Unique Random Numbers in Excel with Dynamic Arrays

Published on

Unique random numbers refer to random numbers without duplicates. Generating such numbers is straightforward in Excel, thanks to dynamic array formulas. These numbers can be used for tasks like random sampling, creating unique IDs, testing data, and more.

Here’s how to generate unique random numbers in Excel with a formula.

Dynamic Array Formula for Unique Random Numbers

Use the following formula to generate unique random numbers in Excel (random numbers without duplicates):

=CHOOSEROWS(
   SORTBY(SEQUENCE(total_n, 1, start_at), RANDARRAY(total_n), 1), 
   SEQUENCE(required_n)
)

Parameters:

  • total_n: The total number of numbers in the sequence.
  • start_at: The starting number of the sequence.
  • required_n: The count of unique random numbers to generate.

Example:

To generate 5 unique random numbers between 1 and 10:

  • total_n = 10
  • start_at = 1
  • required_n = 5

Example: Generating 10 Four-Digit Random Numbers

If you want to generate 10 unique four-digit random numbers:

  • Generate a sequence of numbers from 1000 to 1099 (a total of 100 numbers, though you can expand this range as needed).
  • Randomly pick 10 unique numbers from this list.

Formula:

=CHOOSEROWS(
   SORTBY(SEQUENCE(100, 1, 1000), RANDARRAY(100), 1), 
   SEQUENCE(10)
)

Note: This formula is specific to Excel. It will not work in Google Sheets because Google Sheets does not support the SORTBY function. For a detailed comparison, refer to this resource: SORT and SORTBY – Excel Vs Google Sheets

Generate unique random numbers with an Excel dynamic array formula

Formula Breakdown

Part 1: Generating and Randomizing Numbers

SORTBY(SEQUENCE(100, 1, 1000), RANDARRAY(100), 1)
  1. SEQUENCE(100, 1, 1000):
    • Generates 100 consecutive numbers starting from 1000.
    • Output: {1000; 1001; 1002; ...; 1099}.
  2. RANDARRAY(100):
    • Generates 100 random numbers between 0 and 1 (used for sorting).
  3. SORTBY(SEQUENCE(100, 1, 1000), RANDARRAY(100), 1):
    • Sorts the sequence {1000; 1001; ...; 1099} by the random numbers generated by RANDARRAY.
    • Produces a shuffled array of 100 unique numbers from 1000 to 1099.

Part 2: Selecting n Random Numbers

CHOOSEROWS(..., SEQUENCE(10))
  1. SEQUENCE(10):
    • Creates a sequence from 1 to 10 to select the first 10 rows from the shuffled array.
  2. CHOOSEROWS:
    • Extracts the first 10 numbers from the randomized list.
    • Output: A list of 10 unique, random four-digit numbers.

Why Use This Formula?

This formula is highly versatile and offers several advantages. It supports dynamic updates, meaning that any changes to the parameters—such as the total number of values (total_n), the starting point (start_at), or the required number of outputs (required_n)—automatically update the results.

Additionally, the formula ensures there are no duplicates, guaranteeing that all numbers in the output are unique.

Its flexibility allows you to generate numbers within any range or count, making it suitable for both small lists and large datasets.

Common Applications

Random sampling involves selecting unique samples from a dataset, making it a common use case for generating unique random numbers. In testing scenarios, these numbers are often used to create randomized test data for experiments or software testing. They are also valuable for generating distinct IDs for database records, ensuring that each entry has a unique identifier.

Additionally, unique random numbers play a key role in lottery simulations, where they are used to create randomized number draws for fair and unbiased outcomes.

This approach ensures robust, efficient generation of unique random numbers in Excel, suitable for diverse real-world applications.

Prashanth KV
Prashanth KV
Your Trusted Google Sheets and Excel Expert Prashanth KV is a Diamond Product Expert in Google Sheets, officially recognized by Google for his contributions to the Docs Editors Help Community and featured in the Google Product Experts Directory. Explore his blog to learn advanced formulas, automation tips, and problem-solving techniques to elevate your spreadsheet skills.

Calculate Trip Days by Month (Start, End, and Full Days) in Google Sheets

If you're managing business travel in Google Sheets, you may need to calculate how...

Mode of Comma-Separated Numbers in Excel (Dynamic Array)

There is no dedicated function in Excel to directly find the mode of comma-separated...

How to Find Mode of Comma-Separated Numbers in Google Sheets

Working with comma-separated numbers inside a single cell is a common scenario in Google...

How to Count Merged Cells in Google Sheets (and Get Their Size)

Sometimes, you may have blocks of merged cells in a column or row. But...

More like this

Mode of Comma-Separated Numbers in Excel (Dynamic Array)

There is no dedicated function in Excel to directly find the mode of comma-separated...

Sort Each Row Individually in Excel Using a LAMBDA Formula

Sorting rows in Excel typically refers to rearranging entire datasets based on values in...

Sort by Field Labels Using the SORT and XMATCH Combo in Excel

Want to sort your Excel data by column names instead of column positions? Learn...

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.