Randomize function

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
I think he's saying that you should only run it once in any piece of code, rather than once per session.
 
Upvote 0
I think he's saying that you should only run it once in any piece of code, rather than once per session.


So he's made a mistake?

Rich (BB code):
 "The reason for the above suggestion is that the Randomize statement should only be run once per session... to do otherwise actually limits the number of random values the Rnd function can return."
 
Last edited:
Upvote 0
I wouldn't know, I was going by
Randomize should only be executed once per running of any code that uses the Rnd function call.
 
Last edited:
Upvote 0
It depends on what you're using Rnd() for.

Rnd has a very short cycle, so if you're doing something like generating rolls of a single di, you can accurately predict the entire sequence after watching a dozen or so results. If you were using it to generate alphanumeric passwords, one or two would do. If you want unpredictability, use Randomize often.
 
Last edited:
Upvote 0
Thanks.

The main reason for using Rnd() is to generate as random a number as possible, so the more (Randomize) the merrier!
 
Upvote 0
Within reason. There are a lot of tests for randomness. I expect using Randomize on every call to Rnd would make the results fail some of those tests.
 
Upvote 0
You could instead use [=rand()]. It's much slower, but it has a pattern a million times longer.
 
Upvote 0
Following from Rick Rothstein's comment here:


he suggests, if I understood him correctly, that you should only ever have the randomize function once.


In that case, why not just put it in the Workbook_Open event?
It depends on what you're using Rnd() for.

Rnd has a very short cycle, so if you're doing something like generating rolls of a single di, you can accurately predict the entire sequence after watching a dozen or so results. If you were using it to generate alphanumeric passwords, one or two would do. If you want unpredictability, use Randomize often.
I posted the message at that link before what shg posted was pointed out to me. His suggestion to use [=rand()] (which he posted later in the thread) to obtain a random number is the way to go within VBA code.
 
Upvote 0

Forum statistics

Threads
1,214,950
Messages
6,122,438
Members
449,083
Latest member
Ava19

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top