To generate a random sample in Excel, add a helper column of random numbers with =RAND() next to your data, sort the whole table by that column, and keep the top N rows. That gives a simple random sample without duplicates. For repeatable or stratified samples, use =RANDARRAY() or =RANDBETWEEN() with a fixed seed via =RAND() copied as values, or the Data Analysis ToolPak's Sampling tool. The steps below cover the reliable methods and the traps that make samples biased or non-reproducible.
What You'll Need
- Excel 2016 or later (Microsoft 365 recommended for
RANDARRAYand dynamic arrays). The steps apply to Excel for Windows and Mac; menu labels can differ slightly by version and region. - Your data in a single contiguous table with a header row, and a unique ID column if you have one.
- For the Sampling tool: the Data Analysis ToolPak enabled via File > Options > Add-ins > Manage Excel Add-ins > Go, then tick Analysis ToolPak. On Mac, Tools > Excel Add-ins.
- A decision on sample size and whether you need a simple random sample, a stratified sample, or a reproducible sample.
Step-by-Step
Method 1: Simple random sample with RAND() and sort
- Save a copy of your workbook before sampling. Sorting changes row order, and you want the original intact.
- Insert a helper column to the right of your data. In the first data row, type
=RAND()and press Enter. This returns a decimal between 0 and 1. - Copy the formula down to every data row. Double-click the fill handle or drag it; confirm the last row has a value.
- Select the entire table including the helper column. On the Data tab, click Sort. Choose the helper column as Sort by, keep Sort On as Values, and leave Order as Smallest to Largest. Click OK.
- The rows are now in random order. Select the top N rows for your sample size and copy them to a new sheet. N is your chosen sample size, for example 100 out of 5,000.
- Delete the helper column from the sample sheet, or keep it if you want an audit trail. Do not save over the original file.
Because RAND() recalculates on every edit, the order can change if you sort again. That is fine for a one-off sample but not for reproducible work.
Method 2: Reproducible sample with RAND() frozen as values
- Add the
=RAND()helper column as above and fill it down. - Select the helper column, copy it, then use Paste Special > Values in place. The random numbers are now fixed and will not change when the sheet recalculates.
- Sort by the helper column and take the top N rows. Note the sample size and the date in a cell so you can document the draw.
- If you need to repeat the exact sample later, keep this frozen copy; a new
RAND()draw will not reproduce it.
Method 3: RANDARRAY for a block of random numbers (Microsoft 365)
- In an empty area, enter
=RANDARRAY(ROWS(A2:A5001))to generate one random number per data row. Adjust the range to match your data. - Copy the spilled results and Paste Special > Values into a helper column beside your data so they stop recalculating.
- Sort the table by that column and keep the top N rows, as in Method 1.
RANDARRAY is not available in older Excel versions; use RAND() there.
Method 4: Sampling tool in the Data Analysis ToolPak
- Enable the Analysis ToolPak as described in What You'll Need.
- On the Data tab, click Data Analysis, then Sampling, then OK.
- Set Input Range to your data column (or the ID column). Set Sample Method to Random and enter the number of samples you want. Choose an Output Range or New Worksheet. Click OK.
- The tool returns the sampled values, not whole rows. To get full records, use the sampled IDs with a lookup such as
XLOOKUPorVLOOKUPagainst your original table.
Method 5: Stratified sample
- Add a column for the stratum, for example region or customer tier.
- For each stratum, repeat Method 1 on just that group's rows, taking a proportional number from each. For example, if a region is 20% of the population and you want 100 samples, take 20 from it.
- Combine the stratum samples into one sheet. This ensures small groups are represented rather than being swamped by large ones.
Common Mistakes
- Sorting only the helper column and not the rest of the table, which breaks the link between random numbers and records.
- Forgetting that
RAND()recalculates, so the sample changes after any edit or reopen. - Using
RANDBETWEEN(1, N)to pick row numbers and getting duplicates, which biases the sample toward repeated picks. - Sampling from a filtered or partially selected range and assuming it covered all rows.
- Overwriting the original file after sorting, losing the source order.
- Using the Sampling tool's output as full records when it only returns the sampled column values.
Tips
- Keep a copy of the original data and a note of the sample size, method and date.
- Freeze random numbers with Paste Special > Values before sorting if you need an auditable, repeatable draw.
- Use a unique ID column and sample IDs, then look up full records; this keeps the draw clean and easy to verify.
- For large tables, sort once and take the top N rather than copying random rows one at a time.
- Check the sample's distribution against the population for key columns to catch accidental bias.
Troubleshooting
- The sample keeps changing.
RAND()is volatile. Paste the helper column as values before sorting, or accept that each recalculation is a new draw. - Sort scrambles the table. You selected only part of the range. Select the whole table including the helper column, then sort.
- Data Analysis is missing. The ToolPak is not enabled. Enable it via File > Options > Add-ins, or use Method 1 instead.
- RANDARRAY returns #NAME?. Your Excel version does not support it. Use
RAND()filled down. - Duplicates appear. You used row-number picking with replacement. Switch to the sort method, which draws without replacement.
- The sample looks skewed. Check whether you sampled from a filtered range or a single stratum; redo the draw across all rows or stratify deliberately.
Frequently Asked Questions
Can I generate a random sample without sorting?
Yes. Add a RAND() column, then use FILTER with RANK.EQ or LARGE to pull the top N rows into a new range. Sorting is simpler and less error-prone for most users.
Does RAND() produce true random numbers?
No. It is a pseudo-random generator seeded by Excel. It is fine for sampling and simulation, but not for cryptography or security tokens.
How do I make the sample reproducible?
Freeze the random numbers as values before sorting, and record the sample size and date. Excel's RAND() does not accept a seed, so you cannot regenerate the same sequence from a formula alone.
Can I sample whole rows rather than single values?
Yes. Put the helper column beside the full table, sort the whole table by it, and take the top N rows. The Sampling tool returns only the selected column, so use the sort method for full records.
What sample size should I use?
That depends on your analysis and population size; there is no single Excel setting. Decide the size before drawing, and keep it consistent across strata if you stratify.
Why does my sample include the header row?
The header was included in the sort or sample range. Exclude the header row from the helper column and from the Sampling tool's input range.