VB6随机化函数?

发布于 2024-11-19 02:57:30 字数 396 浏览 6 评论 0原文

我正在处理一些(混乱)遗留代码,并且我在 Form_Load 中遇到了这个片段:

[other code]

Dim r As Byte
Dim g As Byte
Dim b As Byte
Randomize

[more code]

我对 VB 的许多部分还比较陌生,所以请原谅如果这是公然打你的脸,那是我的无知,但有人能告诉我这是怎么回事吗?我对变量声明很满意,这些很有意义。但“随机化”只是在那里闲逛做什么呢?这应该是VB的随机函数吗?如果是这样,那么它实际上并没有在这里做任何事情,不是吗?我最初认为它正在调用原始设计者编写的函数/子函数,但不存在类似的东西。程序中没有错误(尽管有数百个无用的行),如果我注释掉“Randomize”,似乎没有任何变化。

I'm working on some (messy) legacy code, and I came across this snippet in Form_Load:

[other code]

Dim r As Byte
Dim g As Byte
Dim b As Byte
Randomize

[more code]

I'm still relatively new to many parts of VB, so please excuse my ignorance if it's blatant and smacking you in the face, but can anyone tell me what's going on here? I'm fine with the variable declarations, those make sense. But what is "Randomize" just doing hanging out there? Is this supposed to be VB's randomize function? If so, it's not actually doing anything here, is it? I initially thought it was calling a function/sub that the original designer(s) wrote, but nothing like that exists. There are no errors in the program (though there are hundreds of useless lines) and if I comment "Randomize" out, nothing seems to change at all.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情未る 2024-11-26 02:57:30

随机 使用当前系统时间作为随机数生成器的种子(如果您习惯使用 C 或 C++,则对应于 srand(time(NULL)))。与任何其他语言一样,随机生成器通常应该只播种一次(在应用程序启动时),但一个常见的错误是将其播种到重复调用的某个方法中,所以如果是这种情况,我不会感到惊讶。

但请注意,如果不调用 Randomize,则每次程序启动时都会生成相同的随机数序列。

Randomize seeds the random number generator with the current system time (corresponds to srand(time(NULL)) if you're used to C or C++). Like in any other language, the random generator should normally only be seeded once (at application startup), but a common mistake is to seed it inside some method that is called repeatedly, so I wouldn't be surprised if that is the case here.

Note, though, that without any call to Randomize, the same sequence of random numbers will be generated each time the program starts.

何处潇湘 2024-11-26 02:57:30

随机化随机数生成器的种子。如果函数“Rnd”(生成随机数)出现在代码中的其他位置,则 Randomize 很可能不是无用的。

Randomize seeds the random number generator. If the function "Rnd" (which generates a random number) appears elsewhere in the code, Randomize is very likely not useless.

故人的歌 2024-11-26 02:57:30

随机化[可选种子]为所有后续调用Rnd的随机数生成器种子

Randomize [optional seed] seeds the random number generator for all subsequent calls to Rnd.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文