Randomness in VB6 (Part II)
RandomizeOne uninteresting thing to note. The first number that Rnd has returned so far has been the same 0.7055475 that we are used to. How might we get a number other than 0.7055475? Perhaps there might be a way to call Rnd to pass up a few numbers. Like maybe, Rnd X = Rnd (this one will be 0.533424). Sure, passing up one Rnd will cause a different number to be first, but then, how would we get something different from 0.7055475 and 0.533424 to show up? Rnd Rnd X = Rnd (this one will be 0.5795186). And then, maybe For LV = 1 To N Rnd 'Pass up N calls to Rnd. Next X = Rnd But, no matter what number I put in the place of N, it will be the same number everytime the program runs. If only N was a random number: For LV = 1 To Int(Rnd * 20) Rats, I can't do that because Rnd is going to be same number everytime the program runs. This is where Randomize comes in. Randomize 0 will seed the calls to Rnd with the number 0. Whatever that means, it causes a different set of numbers to come from Rnd. Randomize 0 Debug.Print Rnd (0.7641413) That will still give you the same number when the program starts. Similarly, calling Randomize with any constant will cause the same number to appear when the program starts. Luckily, there is a solution: calling Randomize without the number makes Randomize look at the clock and generate a random number from that. Specifically, it looks at the Timer function (returns the seconds since midnight) and gets the seed from there. Randomize Debug.Print Rnd 'This number will be different. which is the same as calling Randomize Timer Debug.Print 'Which is redundant and probably slower since there are two functions involved in the randomization rather than one. 'However, Randomize is pretty fast and should only be called once anyway. Calling Randomize Surely, it is easy to use Randomize. But, do you abuse Randomize? This first example illustrates the use of Randomize Randomize Debug.Print Rnd Debug.Print Rnd Debug.Print Rnd '... The next example illustrates the abuse of Randomize. The following code sample is graphic in nature and may be disturbing to some viewers. Randomize Debug.Print Rnd Randomize Debug.Print Rnd Randomize Debug.Print Rnd You can clearly see Randomize being abused in this example. What are the effects of this abuse? Studies show that this 'de-Randomizes' the numbers from Rnd. What does that mean? Something bad... so don't use it.