用过VBS中RND函数的朋友们可能都发现了一个问题,虽然RND帮助说是随机产生一个数字,但是当你们用这个函数来做随机生成时就会发现,每次产生的数字都是一样的(按照运行代码的先后分),那么我们如何才能实现真正意义的随机呢,以下是我从网上找到了一些相关的代码,并按照我实际项目需求修改过的,分享给大家,如果有问题请各位好友信件我!!!
==================================================================================
Dim i
Dim j
Dim FindstrIndex '实际抽取出来的字符串
Dim WantedValue ' 期望要查找的字段内容
Dim RandomValue '随机值
Dim j
Dim FindstrIndex '实际抽取出来的字符串
Dim WantedValue ' 期望要查找的字段内容
Dim RandomValue '随机值
j = browser("creationtime:=0").page("creationtime:=0").webtable("class:=listView-table").RowCount
For i=1 to j
Dim RandPosition
FindstrIndex=trim(browser("creationtime:=0").page("creationtime:=0").webtable("class:=listView-table").GetCellData(i,2))
WantedValue="ebi"
FindstrIndex=trim(browser("creationtime:=0").page("creationtime:=0").webtable("class:=listView-table").GetCellData(i,2))
WantedValue="ebi"
If FindstrIndex=WantedValue Then
FindstrIndex = browser("creationtime:=0").page("creationtime:=0").webtable("class:=listView-table").GetRowWithCellText(WantedValue)
FindstrIndex = browser("creationtime:=0").page("creationtime:=0").webtable("class:=listView-table").GetRowWithCellText(WantedValue)
If not FindstrIndex=0 Then
RandomValue = RandomNumber(j)
RandPosition = Int(263+(RandomValue*26))
browser("creationtime:=0").page("creationtime:=0").webtable("class:=listView-table").webelement("abs_x:=1115","abs_y:="&RandPosition).click
Exit for
else
Reporter.ReportEvent micfail,"<<font color=red>查询用户信息</font>>","<<font color=red>没有找到预期结果</font>>"
End If
RandomValue = RandomNumber(j)
RandPosition = Int(263+(RandomValue*26))
browser("creationtime:=0").page("creationtime:=0").webtable("class:=listView-table").webelement("abs_x:=1115","abs_y:="&RandPosition).click
Exit for
else
Reporter.ReportEvent micfail,"<<font color=red>查询用户信息</font>>","<<font color=red>没有找到预期结果</font>>"
End If
End If
Next
function RandomNumber(RowCount)
Dim RanArry() '定义随机数数组
Dim BoundIndex ' 数组下标
Dim temp '临时数组
Dim looper '循环控制标志位
Dim upperbound
Dim BoundIndex ' 数组下标
Dim temp '临时数组
Dim looper '循环控制标志位
Dim upperbound
'重定义动态数组
ReDim Preserve RanArry(RowCount)
ReDim Preserve RanArry(RowCount)
'循环将值写入动态数组,下标为最大RowConut数
For looper = 1 To RowCount
RanArry(looper) = looper
Next
For looper = 1 To RowCount
RanArry(looper) = looper
Next
For upperbound = RowCount To 1 Step -1
'抽1~upperbound范围内的随机数作为下标
BoundIndex = Int(upperbound * Rnd + 1)
' msgbox BoundIndex
'抽1~upperbound范围内的随机数作为下标
BoundIndex = Int(upperbound * Rnd + 1)
' msgbox BoundIndex
'把本次用过的换到下次抽出的范围外
temp = RanArry(BoundIndex)
RanArry(BoundIndex) = RanArry(upperbound)
RanArry(upperbound) = temp
RandomNumber = BoundIndex
Exit for
Next
End function
temp = RanArry(BoundIndex)
RanArry(BoundIndex) = RanArry(upperbound)
RanArry(upperbound) = temp
RandomNumber = BoundIndex
Exit for
Next
End function