The best way to generate random numbers with no duplicates in Excel

A blank Excel spreadsheet
Picture: PixieMe/Shutterstock

Over the previous couple of months, I’ve written about a number of of Excel’s new dynamic array capabilities. They’re highly effective and simple to implement—relieving us of some very tiresome expression-building that was vital earlier than. On this article, we’ll mix a number of of those new dynamic array capabilities to generate an array of distinctive random values. It’s straightforward to generate random values; it’s simply as straightforward to restrict the underside and high boundaries for a set of random values. What isn’t really easy, as you’ll see, is producing an array of distinctive random values. Thankfully, utilizing dynamic array capabilities, you solely have to leap by way of a number of hoops.

SEE: 83 Excel ideas each person ought to grasp (TechRepublic)

I’m utilizing Microsoft 365 on a Home windows 10 64-bit system. Dynamic array capabilities are supported by 365, Excel for the Net, Excel 2021, Excel for iPad and iPhone, Excel for Android tablets and telephones. There’s no demonstration file; you gained’t want one.

About random capabilities

Excel has three random worth capabilities: RAND(), RANDBETWEEN(), and RANDARRAY(). RAND() generates random values between 0 and 1, so random decimal values. RANDBETWEEN() allows you to specify the underside and high values. For example, you would possibly use
to generate random integers between 10 and 1,000.

Lastly, RANDARRAY() returns an array of random values utilizing the next syntax:
RANDARRAY([rows], [columns], [min], [max], [integer])
Discover that all the arguments are non-obligatory. These arguments permit you to specify the variety of rows and columns to fill, the minimal and most values to return, and whether or not to return integers or decimals. The integer argument is fake by default, which returns decimal values. To return integers, you should specify TRUE. As well as, min and max are inclusive values. For instance, if these two arguments are 1 and 5, respectively, the operate will return 1, 2, 3, 4 and 5, not simply 2, 3 and 4.

When contemplating arguments, maintain the next guidelines in thoughts:

  • RANDARRAY() performs like RAND(), returning a worth between 0 and 1 in case you omit all arguments.
  • RANDARRAY() performs like RAND(), returning a worth between 0 and 1 in case you omit rows and columns.
  • RANDARRAY() will return 0 or 1 respectively, in case you omit min and max.
  • The min argument should be lower than max.
  • RANDARRAY() will embody decimal values in case you don’t explicitly set integer to TRUE.
  • RANDARRAY() will create the appropriate-sized vary to finish the calculations. This vary known as the recreation vary. If there aren’t enough cells on this vary, RANDARRAY() returns a spill error. To be taught extra about Excel’s spill vary, you may learn The best way to use the spill vary in Excel
  • RANDARRAY() will replace outcomes and resize when the supply information modifications if the supply information is in a Desk object.
  • At the moment, if the information supply is in one other workbook, each workbooks should be open for RANDARRAY() to operate correctly.
  • To generate a random array of non-duplicate values, we’ll use three dynamic array capabilities: RANDARRAY(), UNIQUE(), and SEQUENCE().

The best way to generate random values with out duplicates, type of

RANDARRAY() alone has the potential to return duplicate values, as do all of the random capabilities. If in case you have the time, there’s a easy strategy to obtain random values with no duplicates. Convert the values within the spill vary to static values (additionally changing the operate) after which take away any duplicates utilizing the Take away Duplicates software on the Information tab. It’s straightforward, nevertheless it isn’t an affordable strategy to work until what you’re doing is a one-time cross.

You’ll be able to’t power RANDARRAY() to return solely distinctive values, however you may mix it with UNIQUE() to get the specified outcomes. For example, the next operate, proven in Determine Awill return a single column of 5 (seemingly) distinctive values between 1 and 20:


Two issues would possibly occur although: 1.) You see a listing of random distinctive values that’s lower than you specified; 2.) You see a spill error (to the fitting in Determine A).

When RANDARRAY() repeats sufficient values that the expression can’t return 5 distinctive values from RANDARRAY()’s pool of random values, it should return what it might probably—which may be brief a worth or two. The probabilities of having so many repeating values in that pool of 20 (the min and max values of 1 and 20, respectively) that you may’t return a listing of 5 is small, however not unimaginable. Press F9 and watch the expression replace; finally you will notice an array that’s lower than 5 digits.

The shot on the fitting (Determine A) exhibits the expression within the components bar the place I’ve manually calculated RANDARRAY() by deciding on solely that operate and urgent F9. As you may see, the array does repeat a worth, the worth 1. Therefore, the expression can return solely 4 distinctive values.

Determine A

You would possibly count on to see 5 distinctive values.

Determine B exhibits a spill error, spill vary unknown. I’ve modified the rows argument from 5 to 19 forcing that to occur extra usually, however in case you press F9 usually sufficient, the rows argument of 5 will finally return a spill error. It has to do with the risky nature of those capabilities—generally one calculates sooner than the opposite. A risky operate recalculates each time a change is made.

Determine B

Spill errors are potential with this straightforward expression.

A method to make sure that the expression doesn’t return fewer values than specified is to create an enormous pool of random values. Maybe the best manner is to vary the rows argument from a selected worth to a brief expression: n^2. For instance,

=UNIQUE(RANDARRAY(5^2, 1, 1, 20,TRUE))

The row expression, 5^2, will return 25 random values, however now the expression returns as much as 25 values (rows) and also you’re nonetheless inclined to an occasional spill vary error. Nonetheless, it’s unlikely to return fewer than 5 rows.

For those who can reside with all these behaviors, you might cease right here. Chances are high, although, that you just’ll need one thing extra steady.

The best way to generate random values with out duplicates

Within the final part, you discovered so much about RANDARRAY() and UNIQUE() and the way they work collectively, although the outcomes aren’t steady. Your wants will decide whether or not you should take this downside to the subsequent stage.

Proper now, we have now an expression that doesn’t all the time return the fitting variety of values and has the potential to return a spill error. To keep away from these two issues, you want a extra complicated expression within the type

=INDEX(UNIQUE(RANDARRAY(rows^2, columns, min, max, TRUE)), SEQUENCE(rows))

It seems horrible, however don’t fear. When you perceive the way it works, it should make good sense, and also you’ll have little bother making use of it to your personal work.

RANDARRAY() generates an array of random values primarily based on min and max. The rows argument to the facility of two generates a big pool of random values—a lot bigger than simply the rows worth itself. UNIQUE() removes all duplicate values and returns an array of distinctive values. We acquired this far within the final part.

The distinction this time, is that UNIQUE() doesn’t return the array to the sheet. As soon as UNIQUE() removes duplicates, INDEX() makes use of the variety of rows expressed in SEQUENCE() to find out what number of distinctive random values to return as an array to the sheet.

Determine C exhibits this expression utilizing our earlier argument values:


First, RANDARRAY() returns a single-column array of 25 random integers between 1 and 10. UNIQUE() removes all of the duplicates from these outcomes. INDEX() then returns the highest 5 distinctive and random values, as decided by SEQUENCE(5).

Determine C

You want a extra steady expression.

Take into account that min and max nonetheless matter. For example, if min and max are 1 and 5, respectively, you may’t return an array of 10 values—there aren’t that many distinctive values between 1 and 5. The expression will return 5 distinctive values, however the remaining cells within the spill vary will show errors. That’s an over-simplification, nevertheless it makes the purpose.

As well as, giant arrays take time. Think about a rows worth of 2000^2. Internally, Excel is calculating 4,000,000 random numbers! You in all probability don’t want that enormous of a pool. As an alternative, multiply the rows argument by one other worth, comparable to 10. That returns 20,000 random values, which may be sufficient, depending on the min and max values.

You’ll must experiment to find out whether or not a pool is giant sufficient. There’s no true silver bullet due to the connection: the possibility or returning an incomplete spill vary will increase because the variety of random numbers handed to UNIQUE() decreases.

It’s confession time. This can be a inventive and highly effective expression that takes benefit of Excel’s newer dynamic array capabilities. I’d like to say full credit score for designing this expression, nevertheless it has been across the web now for some time. If I may credit score the inventive genius who got here up with it the primary time, I might.

To be taught extra about dynamic array capabilities, learn the next articles:

Leave a Comment