开发者

VB6 Randomize function?

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.


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜