-1

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 ?

AkkixDev
  • 450
  • 3
  • 12

1 Answers1

4

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

John Coleman
  • 51,337
  • 7
  • 54
  • 119
  • Mid(typext, ((tmpext - 1) * 3) + 1, 3) What exactly is above numbers performing the extraction of those extension ? How will such multiplication and addition whatsoever will produce such result ? – AkkixDev Apr 25 '16 at 10:17
  • The middle argument `((tmpext - 1) * 3) + 1` evaluates to one of the numbers 1,4,7,...,31 (depending on the random value `tmpext`). If `tmpext` is 2, for example, then this will evaluate to 7 and then `Mid(typext,7,3)` would extract the 3 characters which start at position 7. That would be the substring "bat". As I indicated in my answer, I really don't think this is a good way to go about this. I mentioned that I thought it too inflexible. It is also needlessly obscure. – John Coleman Apr 25 '16 at 10:24
  • Because of the subtraction by 1, in the above comment I should have said that if `tmpext` is *3* then the expression evaluates to 7... – John Coleman Apr 25 '16 at 10:48
  • I want to call a function and it's chance should be rare what's wrong ? 'Generating random numbers for probability purpose: 'Code start `Function random(v1,v2) Randomize lol =(Int((v2 - v1 + 1)* Rnd + v1)) End Function '--Calling the function do Call random(100,1000) If lol >700 And lol <760 Then Call download() End If loop` – AkkixDev Apr 25 '16 at 11:02
  • 1
    This is really a separate question, so perhaps you could post it as another question on Stack Overflow. When you do so, don't just give the code and ask "what's wrong?" Describe the error that you are encountering in more detail. What is the expected output? Actual output? If you have a run-time error, what line causes the error and what does the error say? – John Coleman Apr 25 '16 at 11:06