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.
Randomizeseeds the random number generator with the current system time (corresponds tosrand(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.