理解Excel VBA中的randomize语句

3
我正在开发一个小程序,它可以生成标准的、正态分布的数字,需要给出一个均匀分布的随机数源。因此,我需要生成一堆随机数。我决定使用RND函数,因为程序应该尽可能快(所以没有额外的种子函数我想使用)。
经过一些研究,我发现使用Randomize语句可以更有效地使用RND函数。我不明白可选的数字参数的描述。我了解到如果我不给Randomize函数任何参数,它将使用系统计时器值作为新的种子值。
有人能向我解释一下这个可选数字实际上是如何与函数配合使用的吗?使用Randomize(1)Randomize(99)或者Randomize("blabla")之间有什么区别吗?我想了解这个可选输入数字背后的理论。谢谢!

enter image description here

2个回答

2
种子用于初始化伪随机数生成器。基本上,种子用于生成伪随机数,您可以将其视为生成随机数的“起点”。如果种子发生变化,则数字的随机性会增加,这就是为什么默认使用当前系统时间(因为它是连续变化的)的原因。
从您发布的MSDN文章的注释中:
Randomize使用数字来初始化Rnd函数的随机数生成器,给它一个新的种子值。如果省略数字,则使用系统计时器返回的值作为新种子值。
因此,如果指定参数,您将始终拥有相同的种子,从而降低随机性。
如果未使用Randomize,则Rnd函数(无参数)第一次调用时使用相同的数字作为种子,之后使用上一次生成的数字作为种子值。
在这里,我们使用最后生成的随机数作为种子,这增加了随机性。

2

以下是摘自CrossValidated上一个非常类似问题的引用:

Most pseudo-random number generators (PRNGs) are build (sic) on algorithms involving some kind of recursive method starting from a base value that is determined by an input called the "seed". The default PRNG in most statistical software (R, Python, Stata, etc.) is the Mersenne Twister algorithm MT19937, which is set out in Matsumoto and Nishimura (1998). This is a complicated algorithm, so it would be best to read the paper on it if you want to know how it works in detail. In this particular algorithm, there is a recurrence relation of degree $n$, and your input seed is an initial set of vectors x0, x1, ..., xn-1. The algorithm uses a linear recurrence relation that generates:

xn+k = f(xk, xk+1, xk+m, r, A)

where 1 <= m <= n and r and A are objects that can be specified as parameters in the algorithm. Since the seed gives the initial set of vectors (and given other fixed parameters for the algorithm), the series of pseudo-random numbers generated by the algorithm is fixed. If you change the seed then you change the initial vectors, which changes the pseudo-random numbers generated by the algorithm. This is, of course, the function of the seed.

Now, it is important to note that this is just one example, using the MT19937 algorithm. There are many PRNGs that can be used in statistical software, and they each involve different recursive methods, and so the seed means a different thing (in technical terms) in each of them. You can find a library of PRNGs for R in this documentation, which lists the available algorithms and the papers that describe these algorithms.

The purpose of the seed is to allow the user to "lock" the pseudo-random number generator, to allow replicable analysis. Some analysts like to set the seed using a true random-number generator (TRNG) which uses hardware inputs to generate an initial seed number, and then report this as a locked number. If the seed is set and reported by the original user then an auditor can repeat the analysis and obtain the same sequence of pseudo-random numbers as the original user. If the seed is not set then the algorithm will usually use some kind of default seed (e.g., from the system clock), and it will generally not be possible to replicate the randomisation.

根据你问题中的引言,VBA的随机函数将为函数设置一个新的种子。如果您提供了该函数的参数,它将使用该数字作为的新种子;否则,它将使用系统时间作为种子。如果在调用函数之前没有调用Randomize函数,则函数将使用上一个数字作为新的种子,因此可能会得到相同的数字序列。
我还建议查看这个答案

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接