[英]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 . 我正在访问Vxheaven.org,而我却发现此代码带有一个随机文件名。
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 . 我对这些随机语句及其与for循环的用法感到困惑。 Can anyone explain me what is actually happening here ?
谁能解释一下这里的实际情况?
The purpose of Chr(Int(Rnd(1) * 26) + 97)
is to pick a random character in the range "a" to "z". Chr(Int(Rnd(1) * 26) + 97)
是选择一个在“ a”到“ z”范围内的随机字符。 It works because the ascii code for "a" is 97, with the rest of the alphabet following in order. 之所以有效,是因为“ a”的ascii码为97,其余字母依次排列。 Thus the For loop builds up a random lower case string whose length is somewhere between 1 and 20.
因此,For循环会建立一个随机的小写字符串,其长度在1到20之间。
typext = "execombatbmpjpggifdocxlsppthtmhtthta"
is a string of 33 = 3x11 characters. 是33 = 3x11个字符的字符串。 Successive triples are common file extensions, "exe", "com", "bat", etc. The expression
连续的三元组是常见的文件扩展名,例如“ exe”,“ com”,“ bat”等。
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. 1)
Randomize (Timer)
第一次是不必要的冗长。 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. Randomize
分配带有系统时间的随机数生成器-您不需要传递任何东西,除非您希望将来能够重现随机数流,这不是这种情况。
2) Randomize (Timer)
the second and third time is really pointless. 2)第二次和第三次
Randomize (Timer)
确实没有意义。 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. 由于
Timer
具有1毫秒的分辨率,因此再次使用该行可能会将随机数生成器重置为完全相同的种子。 Thus the repetitions of that line could well decrease the amount of randomness in the output. 因此,该行的重复可以很好地减少输出中的随机性。
3) In Rnd(1)
the 1
is pointless. 3)在
Rnd(1)
中1
是没有意义的。 It has exactly the same output as Rnd
它的输出与
Rnd
完全相同
4) Why hardwire in 11 specific file extensions and why restrict yourself to file extensions of length 3? 4)为什么要硬编码11个特定的文件扩展名,为什么要限制自己使用长度为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. 这样,您可以将其他条目自由添加到数组中,包括诸如“ c”之类的内容,其余代码将起作用。
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
典型输出:
bgwkxjvaapr.exe.vbs
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.