How-to recipes · Analysis

How to pick a random sample from a list in Excel

Select random items without repeats using SORTBY, RANDARRAY and TAKE in Microsoft 365, or RAND with a helper column in older Excel, and freeze the result.

Intermediate RANDARRAY SORTBY TAKE ROWS RAND RANDBETWEEN INDEX MATCH SMALL RANK.EQ COUNTA

Auditors check a random sample of invoices, teachers pick students at random to present, managers draw prize winners, researchers select survey participants. In all these cases you need several items picked without repeats, and you usually need to keep the result fixed once it is drawn. Excel's random functions recalculate every time anything changes, which is the most common surprise in this task. This recipe shows how to draw the sample and how to freeze it.

The idea: shuffle, then take the top

The reliable way to sample without repeats is to give every item a random number, sort the list by that number and take the first n items. Since every item gets exactly one random number, no item can be chosen twice. Picking random positions with RANDBETWEEN, by contrast, can pick the same position twice.

Microsoft 365: one formula

RANDARRAY creates one random number per row, SORTBY shuffles the list with them, and TAKE keeps the first three:

Formula
=TAKE(SORTBY(A2:A9,RANDARRAY(ROWS(A2:A9))),3)

Every time the sheet recalculates (after any edit or when you press F9), a new sample appears. That is ideal for testing, but not for a documented audit sample. See the section on freezing below.

Excel 2016 and 2019: helper column with RAND

  1. In B2 type =RAND() and copy it down to B9. RAND returns a random number between 0 and 1.
  2. In D2 type the formula below and copy it down to D4 for a sample of three.
  3. Each row returns the employee with the smallest, second smallest and third smallest random number.
Formula
=INDEX($A$2:$A$9,MATCH(SMALL($B$2:$B$9,ROWS($D$2:D2)),$B$2:$B$9,0))

SMALL finds the k-th smallest random number, MATCH locates its row and INDEX returns the name. ROWS($D$2:D2) counts 1, 2, 3 as you copy down. A simpler alternative is to put =RANK.EQ(B2,$B$2:$B$9) next to each name and pick the rows ranked 1 to 3.

Freeze the sample so it stops changing

Select the random numbers in column B, copy them and paste them back as values (Home > Paste > Values). They no longer change, and the sample becomes fixed. Here is the result of the formulas above with frozen random numbers:

D2fx=INDEX($A$2:$A$9,MATCH(SMALL($B$2:$B$9,1),$B$2:$B$9,0))
ABCD
1EmployeeRandom
2Ana0.731Gus
3Ben0.112
4Chloe0.958
5Dev0.404
6Eva0.267
7Femi0.845
8Gus0.059
9Hana0.583

The random numbers were pasted as values. The smallest is 0.059 (Gus), then 0.112 (Ben) and 0.267 (Eva).

Result: Gus

With the frozen numbers in B, the Microsoft 365 version becomes reproducible too: sort by column B instead of RANDARRAY.

D2fx=TAKE(SORTBY(A2:A9,B2:B9),3)
ABCD
1EmployeeRandom
2Ana0.731Gus
3Ben0.112Ben
4Chloe0.958Eva
5Dev0.404
6Eva0.267
7Femi0.845
8Gus0.059
9Hana0.583

Same sample as the INDEX/MATCH formula, in one step.

For an audit, keep the frozen random numbers in the file: they document how the sample was drawn. Alternatively, copy the final sample and paste it as values in a new sheet.

Variations

  • Sample size in a cell: replace 3 with a cell reference such as F1 so users can choose how many to draw.
  • Percentage sample: to draw 10% of the list, use TAKE(...,ROUNDUP(ROWS(A2:A9)*10%,0)).
  • One random item: =INDEX(A2:A9,RANDBETWEEN(1,ROWS(A2:A9))) with RANDBETWEEN is fine when you only need one pick.
  • Random sample of whole records: use the full table, =TAKE(SORTBY(A2:D9,RANDARRAY(ROWS(A2:D9))),3), to keep all columns together.
  • Variable-length list: use COUNTA to count the filled rows, or point the formula at an Excel table column.

Pitfalls

  • Recalculation. RAND, RANDBETWEEN and RANDARRAY are volatile: they change on every edit anywhere in the workbook. Freeze them before you print, save or share a sample.
  • Duplicates in the list. If the same name appears twice in the source, it can appear twice in the sample. Remove duplicates first if each person should have one chance (How to remove duplicates in Excel).
  • Ties in random numbers are practically impossible with RAND (15 digits), but can happen if you round them. Do not round the helper column.
  • Not cryptographic. Excel's generator is fine for sampling and games, not for lotteries with legal requirements or security purposes.
Tip. The Analysis ToolPak (File > Options > Add-ins) also has a Sampling tool, but it samples with replacement, so the same item can appear twice. The formulas above are usually the better choice. More on dynamic arrays in Dynamic arrays: spill, FILTER, SORT, UNIQUE, SEQUENCE and #.
⬇Practice workbookExamples with live formulas + practice sheets · .xlsx · 15 KB

More how-to recipes