Is there something wrong with the random generator in Excel?
"Rick Rothstein" wrote:
See the Note in the Remarks section of the Help Files for either the
Randomize or Rnd functions.
D'oh! I read EricG's explanation literally [1]. I spotted-checked the Help
page, but I did not look at all of the details.
Empirically, I discovered that Rnd -1 (any negative number) is also needed
in order for multiple calls to Randomize (no argument) to repeat the same
sequence of pseudorandom numbers when called within the same 15.625-msec
window. (System time is updated every 15.625 msec, at least in MSWin XP.)
Not very useful, of course. But that explains why calling Randomize alone
in a tight loop seemed to work suprisingly well, albeit not a good practice.
It appears that Randomize uses some state information (probably the PRNG
factors after the last Rnd call) in conjunction with its argument, or with
"system time" when there is no argument. Calling Rnd -1 first ensures a
consistent state, ergo a repeatable seed. Without calling Rnd -1
before Randomize, the PRNG state is inconsistent, resulting in an
unpredictable seed despite the same or similar "system time".
-----
[1] EricG wrote: ``If you are using something like "Randomize 1234568",
then you will get the same "random" numbers each time you run your code.``
----- original message -----
"Rick Rothstein" wrote in message
...
Let me be more clear. When "Randomize 123" is put into the ThisWorkbook
Workbook_Open macro, the first Rnd value is indeed the same each time the
workbook is opened.
But subsequent calls to "Randomize 123" result in different first Rnd
values, whether those calls are in Workbook_Open or in a public macro
like
testRandomize2 below.
Am I doing something wrong?(!)
See the Note in the Remarks section of the Help Files for either the
Randomize or Rnd functions.
--
Rick (MVP - Excel)
|