经过一些研究,我发现使用Randomize语句可以更有效地使用RND函数。我不明白可选的数字参数的描述。我了解到如果我不给Randomize函数任何参数,它将使用系统计时器值作为新的种子值。
有人能向我解释一下这个可选数字实际上是如何与函数配合使用的吗?使用
Randomize(1)
和Randomize(99)
或者Randomize("blabla")
之间有什么区别吗?我想了解这个可选输入数字背后的理论。谢谢!
Randomize(1)
和Randomize(99)
或者Randomize("blabla")
之间有什么区别吗?我想了解这个可选输入数字背后的理论。谢谢!
以下是摘自CrossValidated上一个非常类似问题的引用:
根据你问题中的引言,VBA的随机函数将为函数设置一个新的种子。如果您提供了该函数的参数,它将使用该数字作为的新种子;否则,它将使用系统时间作为种子。如果在调用函数之前没有调用Randomize函数,则函数将使用上一个数字作为新的种子,因此可能会得到相同的数字序列。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.