返回列表 发帖

[问题求助] 为何此VBS生成的随机数中那么多重复的?

function randomchar(tlen)
dim tmp,i,j
for j=1 to tlen step 1
'i=randomnumber.value(1,3) 'QTP的用法,不适用于vbs
Randomize
i=int((3*rnd) +1)
'1-大写字母,2-数字,3-小写字母
select case i
Case 1
Randomize
tmp=int(26*rnd)
tmp=tmp+97
case 2
Randomize
tmp=int(10*rnd)
tmp=tmp+48
case 3
Randomize
tmp=int(26*rnd)
tmp=tmp+65
case else
msgbox("function:randomchar(tlen) is wrong!")
end select
randomchar=randomchar&chr(tmp)
Next
end Function
Dim fso,myfile,i
Set fso=CreateObject("scripting.filesystemobject")
Set myfile=fso.CreateTextFile("C:\Documents and Settings\Administrator\Desktop\123.txt",True,True)
i=0
Do
myfile.WriteLine(randomchar(14))
i=i+1
If i>1200 Then Exit Do
Loop Until i>1000COPY
应该说不会产生太多相同随机数的,但是从打印结果中发现好多相同的,而且是有规律的右移,如下是某次产生的随机数,是搜索sn时的部分结果
        Line 789: SN5iqT02P06D72
        Line 798: y7SN5iqT02P06D
        Line 807: mGy7SN5iqT02P0
        Line 816: jvmGy7SN5iqT02
        Line 825: oZjvmGy7SN5iqT
        Line 834: BFoZjvmGy7SN5i
        Line 843: PmBFoZjvmGy7SN

        Line 853: SN5iqT02P06D72
        Line 862: y7SN5iqT02P06D
        Line 871: mGy7SN5iqT02P0
        Line 880: jvmGy7SN5iqT02
        Line 896: qvtG1SSN5pqT02
        Line 905: 5ZqvtG1SSN5pqT
        Line 914: BF5ZqvtG1SSN5p
        Line 923: 6tBF5ZqvtG1SSN

不使用随机数表的话,可以这样:
@echo off
set str=0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
for %%a in (%str%) do for %%b in (%str%) do set /a "n+=1"&set @!n!=%%a%%b
(for /l %%a in (1 1 1000) do (
   set /a str=!random!%%1296,str2=!random!%%1296,str3=!random!%%1296,str4=!random!%%1296,st5r=!random!%%1296,str6=!random!%%1296,str7=!random!%%1296
   for %%o in (@!str! @!str2! @!str3! @!str4! @!str5! @!str6! @!str7!) do echo !%%o!!%%p!!%%q!!%%r!!%%s!!%%t!!%%u!
))>随机字符串.txtCOPY
如果量比较大,建议先生定义随机数表为一个变量,再分段截取,也可获得随机字符串

TOP

本帖最后由 batman 于 2011-3-25 14:53 编辑

代码修改如下:
function randomchar(tlen)
        dim tmp,i,j
        for j=1 to tlen                step 1
                'i=randomnumber.value(1,3)        'QTP的用法,不适用于vbs
                Randomize
                i=int((3*rnd) +1)
                '1-大写字母,2-数字,3-小写字母
               
                select case i
                        Case 1
                                Randomize
                                tmp=int(26*rnd)
                                tmp=tmp+97
                        case 2
                                Randomize
                                tmp=int(10*rnd)
                                tmp=tmp+48
                        case 3
                                Randomize
                                tmp=int(26*rnd)
                                tmp=tmp+65
                        case else
                                msgbox("function:randomchar(tlen) is wrong!")
                end select
                vbstr=vbstr&chr(tmp)
        Next
end Function
Dim fso,myfile,i
Set fso=CreateObject("scripting.filesystemobject")
Set myfile=fso.CreateTextFile("C:\Documents and Settings\Administrator\Desktop\123.txt",True,True)
i=0
Do
vbstr = ""
randomchar(14)
myfile.WriteLine(vbstr)
i=i+1
If i>1200 Then Exit Do
Loop Until i>1000COPY
***共同提高***

TOP

要不然就在dim tmp,i,j下面加上randomchar = ""来清空变量
***共同提高***

TOP

4# batman
你的这两种方式都试过,仍然存在蛮多的重复现象,还是不清楚是哪里出了问题,以前看C#的时候见过有讨论,可能和时间种子之类有关,我也在继续找找原因

TOP

会不会是需要更换随机数的种子?

TOP

VBS的随机数种子是以一天内的时间(小时,分钟,秒)来生成的,一天内重复概率少,但是如果你多跑几天重复就多了,还有就是如果你多开几台机器跑,重复率也很高,因为这个原因,微软设计COM的GUID的时候,把当前公历时间+机器特征,作为种子,才避免了重复现象

TOP

返回列表