|
'还是跳蚤算法(same as Zamyi)好。字典多了就慢了,以下代码在原贴上仅供参考。
Option Explicit
Sub DictionaryRnd()
Dim i As Long, dic
Dim min As Long, max As Long, areaNum As Long
Dim t
t = Timer
max = range("d2").Value
min = range("d1").Value
Set dic = CreateObject("scripting.dictionary")
areaNum = max - min
Do Until dic.Count = range("d3").Value
i = Int(Rnd * areaNum) + min
dic(i) = dic(i)
Loop
[d4] = Timer - t
range("A:A").Clear
range("a1").Resize(range("d3").Value) = Application.Transpose(dic.keys)
Set dic = Nothing
End Sub
Sub ArrayRnd()
Dim arr, brr, i, j
Dim min As Long, max As Long
Dim tmpRnd As Long, temp As Long
Dim t
t = Timer
max = range("d2").Value
min = range("d1").Value
ReDim arr(max - min)
For i = min To max
arr(i - min) = i
Next i
Randomize Timer
ReDim brr(1 To range("d3").Value,1 To 1)
j = UBound(arr)
For i = 1 To range("d3").Value
tmpRnd = Int(Rnd * j)
brr(i, 1) = arr(tmpRnd)
temp = arr(tmpRnd)
arr(tmpRnd) = arr(j)
arr(j) = temp
j = j - 1
Next i
[d4] = Timer - t
range("A:A").Clear
range("a1").Resize(range("d3").Value) = brr
Set arr = Nothing
Set brr = Nothing
End Sub
[ 本帖最后由 yes2boy 于 2011-1-24 11:57 编辑 ] |
|