Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel Random 4 Digit Number: How to Generate

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To generate a random 4-digit number in Excel, use the formula =RANDBETWEEN(1000,9999). Type it into any cell and press Enter. This gives you a whole number between 1000 and 9999, which is always four digits. If you need to keep the number from changing every time the sheet recalculates, copy the cell and paste it back as a value. That is the short answer. The rest of this guide covers variations, common pitfalls, and how to generate many numbers at once.

What You'll Need

  • Microsoft Excel (any recent version for Windows, Mac, or the web). The steps are the same across platforms unless noted.
  • A blank worksheet or an existing sheet where you can add a column.

Step-by-Step

  1. Select a cell. Click any empty cell where you want the first random number to appear. For example, cell A1.
  2. Enter the formula. Type =RANDBETWEEN(1000,9999) and press Enter. The cell now shows a random four-digit number.
  3. Fill down for more numbers. If you need a list, click the cell, grab the fill handle (the small square at the bottom-right corner), and drag down as many rows as you need. Each cell will get its own random number. Alternatively, copy the cell and paste it into other cells.
  4. Convert to static values (optional but recommended). RANDBETWEEN recalculates every time the sheet changes or you press F9. To freeze the numbers, select the cells, press Ctrl+C (Cmd+C on Mac), then right-click and choose Paste Special > Values. This replaces the formulas with the current numbers.
  5. Format as text if leading zeros matter. RANDBETWEEN never returns a number below 1000, so you won't get leading zeros. If you need a four-digit string that could start with zero (e.g., 0123), use a different approach: =TEXT(RANDBETWEEN(0,9999),"0000"). This returns a text value with exactly four digits, padding with zeros as needed.

Common Mistakes

  • Forgetting that RANDBETWEEN recalculates. Many people generate numbers, then edit another cell and find their numbers have changed. Always convert to values if you need a fixed set.
  • Using RAND instead of RANDBETWEEN. RAND gives a decimal between 0 and 1. To get a four-digit integer with RAND, you would need a more complex formula like =INT(RAND()*9000)+1000. RANDBETWEEN is simpler and less error-prone.
  • Expecting duplicates to be impossible. RANDBETWEEN can produce the same number more than once, especially in small lists. If you need unique numbers, you must check for duplicates or use a different method (see Tips).
  • Assuming RANDBETWEEN is available in all versions. It is included in Excel 2007 and later. In older versions, you would use =INT(RAND()*9000)+1000. Also, RANDBETWEEN requires the Analysis ToolPak in very old versions (pre-2007), but that is rarely a concern now.
  • Typing the formula with a space or wrong comma. Excel is picky about syntax. Use a comma between the arguments, not a semicolon (unless your region uses semicolons as separators). If you see a #NAME? error, check that you spelled RANDBETWEEN correctly.

Tips

  • Generate a list without duplicates. If you need unique four-digit numbers, you can use a helper column with =RAND(), sort by that column, then assign numbers sequentially. Or use a formula like =RANDBETWEEN(1000,9999) and then use Conditional Formatting to highlight duplicates, manually replacing them. For larger sets, consider using =UNIQUE(RANDBETWEEN(1000,9999)) in Excel 365, but note that UNIQUE on a single value does not prevent duplicates across rows; you would need to generate an array. A practical approach: generate a long list, then use Data > Remove Duplicates.
  • Force a new set of random numbers. Press F9 to recalculate the sheet. This gives you a fresh set of numbers from any RANDBETWEEN formulas.
  • Use a specific range. If you need numbers from 0000 to 9999 as text, use the TEXT formula shown earlier. If you need numbers from 1000 to 9999 but want to exclude certain values, you can use =RANDBETWEEN(1000,9999) and then filter or use IF to reject unwanted numbers.
  • Generate numbers in bulk with a single formula. In Excel 365, you can enter =RANDBETWEEN(1000,9999) in a cell and then use the fill handle to drag down. Or use =RANDBETWEEN(1000,9999) with the new dynamic array behavior: select a range, type the formula, and press Ctrl+Shift+Enter (for older versions) or just Enter (for 365). But the simplest is to drag.
  • Convert to values quickly. Select the range, copy, then use Paste Special > Values. Or drag with the right mouse button and choose "Copy Here as Values Only."

Troubleshooting

  • Formula returns #NAME? Check that you typed RANDBETWEEN correctly. Also ensure that the Analysis ToolPak is enabled if you are on a very old version of Excel (pre-2007). In modern Excel, it is built-in.
  • Numbers change when I don't want them to. This is normal. Convert to values as described in Step 4.
  • I get a decimal instead of a whole number. You might have used RAND instead of RANDBETWEEN. Replace with RANDBETWEEN or wrap RAND in INT: =INT(RAND()*9000)+1000.
  • I need exactly four digits including leading zeros. Use =TEXT(RANDBETWEEN(0,9999),"0000"). This returns text, so if you need to treat it as a number later, be aware that leading zeros will be lost if converted to number.
  • RANDBETWEEN is not available. If you are using Excel 2003 or earlier, you need to enable the Analysis ToolPak via Tools > Add-Ins. Alternatively, use the INT(RAND()) formula.
  • I want to generate numbers without duplicates. There is no built-in function for unique random numbers in a range without using arrays or sorting. A common method: create a list of all possible numbers (1000 to 9999), add a column of RAND(), sort by that column, and take the first N. For a small set, you can use a formula with COUNTIF to check for duplicates and regenerate, but that requires iterative calculation or manual intervention.

Frequently Asked Questions

Can I generate a random 4-digit number that starts with zero?

Yes, but you must treat it as text. Use =TEXT(RANDBETWEEN(0,9999),"0000"). This will produce strings like "0123". If you need it as a number, you cannot preserve the leading zero.

How do I stop the numbers from changing every time I edit the sheet?

After generating the numbers, copy the cells and use Paste Special > Values to replace the formulas with fixed numbers.

Is RANDBETWEEN truly random?

It is pseudo-random, which is sufficient for most purposes like sampling, testing, or games. For cryptographic security, Excel is not the right tool.

Can I generate multiple unique 4-digit numbers at once?

Not with a single formula directly. You can generate a list and then use Data > Remove Duplicates, or use a helper column with RAND() and sort to randomize a sequence of numbers.

What is the difference between RAND and RANDBETWEEN?

RAND returns a decimal between 0 and 1. RANDBETWEEN returns a whole number between two specified integers. For a 4-digit number, RANDBETWEEN is simpler.

Why do I get a #VALUE! error?

Check that both arguments in RANDBETWEEN are numbers. If you accidentally include text or a cell reference that contains text, you will get this error.