Search code examples
for-loopvbscriptrandomstring-lengthdo-loops

Vbscript for loop with len and mid


I was visiting Vxheaven.org , while i found this code to come up with a random file name .

tmpname="" 
randomize(timer) 
namel=int(rnd(1)*20)+1 
For lettre = 1 To namel 
randomize(timer) 
tmpname=tmpname & chr(int(rnd(1)*26)+97) 
Next 
typext = "execombatbmpjpggifdocxlsppthtmhtthta" 
randomize(timer) 
tmpext = int(rnd(1)*11)+1 
tmpname=tmpname & "." & mid(typext,((tmpext-1)*3)+1,3) & ".vbs"

I am confused between these random statements , and its usage with for loop . Can anyone explain me what is actually happening here ?


Solution

  • The purpose of Chr(Int(Rnd(1) * 26) + 97) is to pick a random character in the range "a" to "z". It works because the ascii code for "a" is 97, with the rest of the alphabet following in order. Thus the For loop builds up a random lower case string whose length is somewhere between 1 and 20.

    typext = "execombatbmpjpggifdocxlsppthtmhtthta"
    

    is a string of 33 = 3x11 characters. Successive triples are common file extensions, "exe", "com", "bat", etc. The expression

    Mid(typext, ((tmpext - 1) * 3) + 1, 3)
    

    extracts one of those triples.

    There are many problems with this code.

    1) Randomize (Timer) the first time is needlessly verbose. Randomize by itself seeds the random number generator with the system time -- you don't need to pass it anything unless you want to be able to reproduce the stream of random numbers in the future, which isn't the case here.

    2) Randomize (Timer) the second and third time is really pointless. Since Timer has a 1 millisecond resolution, using that line again is somewhat likely to reset the random number generator to exactly the same seed. Thus the repetitions of that line could well decrease the amount of randomness in the output.

    3) In Rnd(1) the 1 is pointless. It has exactly the same output as Rnd

    4) Why hardwire in 11 specific file extensions and why restrict yourself to file extensions of length 3? It makes more sense to have an array of file extensions and then pick a random element of the array. Something like:

    typext = Array("exe","com","bat","bmp","jpg", "gif", "doc", "xls","ppt", "htm", "htt", "hta")
    r = Int(Rnd * (1+ UBound(typext)))
    tmpname=tmpname & "." & typext(r) & ".vbs"
    

    This way, you can freely add other entries to the array, including things like "c", and the rest of the code will work.

    Here is a cleaned-up version, written as a function:

    Function RandFileName()
        Dim tmpname, namel, lettre, tmpext, typext, r
    
        Randomize
    
        tmpname = ""
        namel = Int(Rnd(1) * 20) + 1
    
        For lettre = 1 To namel
          tmpname = tmpname & Chr(Int(Rnd(1) * 26) + 97)
        Next
    
        typext = Array("exe", "com", "bat", "bmp", "jpg", "gif", "doc", "xls", "ppt", "htm", "htt", "hta")
        r = Int(Rnd * (1 + UBound(typext)))
        tmpname = tmpname & "." & typext(r) & ".vbs"
    
        RandFileName = tmpname
    End Function
    

    Typical output: bgwkxjvaapr.exe.vbs