Advanced Arithmetic
If you're getting heavy-duty with the math in a script, you may need to take advantage of some of VBScript's advanced math functions. These include
Atn():
Arctangent
Cos():
Cosine
Sin():
Sine
Tan():
Tangent
Log():
Logarithm
Sqr():
Square root
Exp():
Returns e (the base of natural logarithm) raised to a power
Randomize:
Randomizes the system's random number generator
Rnd():
Generates a random number
This random number business in particular deserves some explanation, because you may think that'd be a much better way to come up with values for a password. It can be, provided you thoroughly understand how randomness works inside a computer.
First, never forget that computers are giant calculating devices. There's nothing remotely random about anything that goes on inside a computer. As a result, no computer can generate a truly random number without special hardware that's designed to do so.
The Rnd() function returns a value less than 1, but greater than or equal to zero. You can pass a parameter to Rnd() to control its behavior.
A number less than zero, such as Rnd(-2), will return the exact same number every time, using the number you supply as the seed. This isn't random at all. A number greater than zero, such as Rnd(2), will return the next "random" number in the computer's sequence. Zero, or Rnd(0), will return the most recently generated random number again and again.
VBScript's random number generator uses a seed as its initial value, and then calculates pseudorandom numbers from there. Given the same seed, VBScript will always calculate the same sequence of random numbers every time, because they're not random: They're derived from a mathematical formula.
That's where Randomize comes in. This statement seeds the random-number generator, either with a number you supply-guaranteeing a repeatable sequence of "random" numbers-or with a value from the system's timer. Because the system timer has a millisecond resolution, the odds are good that you'll get a unique "random number" sequence every time.
For example, try the following short script.
Randomize 5
For t = 1 to 10
MsgBox Int((6 * Rnd()) + 1)
Next
Run this script on a couple of different computers a couple of different times, and you'll likely get the exact same results every time. Not exactly random, is it? Now, try this modified script.
Randomize
For t = 1 to 10
MsgBox Int((6 * Rnd()) + 1)
Next
The difference here is that the generator is seeded from the system timer, which will virtually guarantee unique-if not necessarily random-results. Using the timer generally creates "random enough" sequences of numbers.
Making a Better Password
Listing 7.2 revises the password-generating function to generate random sequences of uppercase letters.
Listing 7.2. MakePW.vbs. Revised example uses Rnd() and Randomize for better-looking passwords.
Function MakePW()
Dim vPasswd, vTemp, vValue
Randomize
For vTemp = 1 to 8
vValue = Int((26 * Rnd()) + 64)
vPasswd = vPasswd & Chr(vValue)
Next vTemp
MakePW = vPasswd
End Function
This example generates a pseudorandom, eight-character password comprised of uppercase letters.
Making a Better Password-Explained
This example begins much like the previous one, with function and variable declarations.
Function MakePW()
Dim vPasswd, vTemp, vValue
The Randomize statement seeds the generator from the system timer, and then begins a For…Next loop that will run eight times.
Randomize
For vTemp = 1 to 8
First, I calculate a random value from 65 to 91. These are the ASCII values for uppercase A through Z. Remember that the Rnd() function returns a fractional number from zero to less than one; I'm multiplying that by 26, which will give me a result between 1 and 26. I'm adding 64 to the result to get the result in the 65 to 91 range. Finally, I'm using the Int() function to convert the result to a whole number by truncating the decimal portion.
Last, I convert the numeric value to the appropriate ASCII character and append it to the password I'm building.
vValue = Int((26 * Rnd()) + 64)
vPasswd = vPasswd & Chr(vValue)
Finally, I wrap up the loop and end the function.
Next vTemp
MakePW = vPasswd
End Function
There you have it: a "random enough" password.
|