Excel VBA Randomize: How to Use It Correctly
In this article
To get truly different random numbers each time your macro runs, put Randomize on its own line before the first Rnd call. Without it, Excel VBA seeds its random-number generator with the same value every time, so Rnd returns the same sequence on every run. Randomize reseeds the generator from the system timer, which makes each run start from a different point. The one-line fix is: Randomize then Rnd. The rest of this guide covers where to place it, how to get integers in a range, and the mistakes that make it look like it is not working.
What You'll Need
- Excel on Windows or Mac with the Developer tab enabled (File > Options > Customize Ribbon > check Developer, or Excel > Preferences > Ribbon & Toolbar on Mac).
- A macro-enabled workbook (.xlsm) if you want to keep your code.
- Basic familiarity with opening the VBA editor (Alt+F11 on Windows, Option+F11 on Mac) and inserting a module.
Step-by-Step
- Open your workbook and press Alt+F11 (Windows) or Option+F11 (Mac) to open the Visual Basic Editor. Labels in the editor can differ slightly by Excel version and platform, but the VBA language itself behaves the same.
- In the Project window, right-click your workbook name and choose Insert > Module. A blank code window appears.
- Type a short procedure. Start with
Sub RandomTest()on the first line andEnd Subon the last. Everything else goes between them. - Add
Randomizeas the first statement inside the Sub, before anyRndcall. This is the reseed step. It takes no arguments in normal use. - Add your random call on the next line, for example
MsgBox Rnd.Rndreturns a Single between 0 and 1 (never exactly 1). - Press F5 to run it. Run it several times. The displayed value should differ each run because
Randomizereseeded from the timer. - To get a whole number in a range, use the standard formula
Int((upper - lower + 1) * Rnd + lower). For a number from 1 to 100, that isInt((100 - 1 + 1) * Rnd + 1), which simplifies toInt(100 * Rnd + 1). - To fill cells, loop and assign. For example, inside a
For i = 1 To 10loop, writeCells(i, 1).Value = Int(100 * Rnd + 1). KeepRandomizeoutside the loop so you reseed once per run, not on every iteration. - If you need repeatable results for testing, call
Randomize 42with a fixed number instead of no argument. The same seed then produces the same sequence every run, which is useful for debugging but not for real randomness. - Save the workbook as .xlsm (File > Save As, choose Excel Macro-Enabled Workbook) so the code is kept.
Common Mistakes
- Putting
Randomizeinside the loop. Reseeding on every iteration can produce clustered or repeated values because the timer has not moved much between passes. Seed once, then callRndrepeatedly. - Forgetting
Randomizeentirely. The sequence is then identical on every run, which is the classic symptom people report as "random is broken". - Using
Rndwithout scaling.Rndalone gives 0 to just under 1. If you want 1 to 10, you must multiply and add the lower bound. - Off-by-one range errors. Leaving out the
+ 1in(upper - lower + 1)makes the top value impossible to hit. - Expecting cryptographic randomness.
RandomizeandRndare fine for games, sampling and test data, but they are not suitable for security tokens or passwords. - Calling
Randomizein a different procedure than the one usingRnd. The seed is global to the session, so it can work, but it makes the code hard to follow. Keep them together.
Tips
- Use
Randomizeonce at the top of the procedure that owns the random logic. That is the clearest pattern. - For a quick shuffle of a list, combine
Rndwith a temporary sort key column, then sort by that column and delete it. - If you want decimals in a range, use
lower + (upper - lower) * RndwithoutInt. - Store the seed in a cell if you need to reproduce a test run later; write it before calling
Randomize seedValue. - Keep the module free of
Option Expliciterrors by declaring loop counters withDim i As Long.
Troubleshooting
If the same numbers keep appearing, check that Randomize is actually before the first Rnd and is not inside a loop. If values look bunched at one end of your range, recheck the scaling formula and the + 1. If nothing happens at all, confirm macros are enabled (File > Options > Trust Center > Trust Center Settings > Macro Settings) and that you are running the right procedure. If you get a compile error, check for a missing End Sub or a stray character copied from a web page. If results differ between two machines, remember that the underlying generator and timer resolution can vary, so do not expect identical sequences unless you supply a fixed seed.
Frequently Asked Questions
Do I always need Randomize before Rnd?
For anything where you want a different result each run, yes. Without it, the sequence repeats.
Does Randomize work on Excel for Mac?
Yes. The VBA statements are the same; only the editor shortcuts differ.
Can I get random numbers between two decimals?
Yes. Use lower + (upper - lower) * Rnd and skip Int.
Why does Randomize 0 behave differently?
Passing a number uses that value as the seed. Zero is a valid seed and gives a repeatable sequence, which is why it is used for testing.
Is Rnd good enough for passwords?
No. It is not designed for security. Use a dedicated cryptographic method for anything sensitive.
Can I reseed in the middle of a run?
You can call Randomize again, but it is rarely needed and can reduce variety if called rapidly. Seed once per run in most cases.