Private Sub Command2_Click()
Dim tLongs() As Long
tLongs() = RandomArrayGet(10, -10000, 10000)
For tIndex = 0 To UBound(tLongs)
Text2.Text = Text2.Text & " " & tLongs(tIndex)
Next
End Sub
模块:
Type tpActiveArray_Element
aaIndex As Long
aaValue As Long
End Type
Function RandomArrayGet(ByVal pOutCount As Long, ByVal pValueMin As Long, ByVal pValueMax As Long) As Long()
Dim tOutLongs() As Long
Dim tSurArray() As tpActiveArray_Element
Dim tSurArrayBacks() As tpActiveArray_Element
Dim tSwapIndex As Long
Dim tIndex As Long
Dim tValueLength As Long
tValueLength = (pValueMax - pValueMin) + 1
For tIndex = 0 To pOutCount - 1
tSwapIndex = Int(Rnd * tValueLength)
If Not CBool(DummyActiveArray_ValueGetByIndex(tSurArrayBacks(), tIndex)) Then
DummyActiveArray_ValuePutToIndex tSurArrayBacks(), tIndex, -1
DummyActiveArray_ValuePutToIndex tSurArray(), tIndex, tIndex
End If
If Not CBool(DummyActiveArray_ValueGetByIndex(tSurArrayBacks(), tSwapIndex)) Then
DummyActiveArray_ValuePutToIndex tSurArrayBacks(), tSwapIndex, -1
DummyActiveArray_ValuePutToIndex tSurArray(), tSwapIndex, tSwapIndex
End If
DummyActiveArray_ValueSwap tSurArray(), tIndex, tSwapIndex
DummyActiveArray_ValueSwap tSurArrayBacks(), tIndex, tSwapIndex
Next
ReDim tOutLongs(pOutCount - 1)
For tIndex = 0 To pOutCount - 1
tOutLongs(tIndex) = DummyActiveArray_ValueGetByIndex(tSurArray(), tIndex) + pValueMin
Next
Form1.Text1.Text = UBound(tSurArray)
RandomArrayGet = tOutLongs()
End Function
Sub DummyActiveArray_ValueSwap(ByRef pArray() As tpActiveArray_Element, ByVal pDummyIndexA As Long, ByVal pDummyIndexB As Long)
Dim tTempElement As tpActiveArray_Element
Dim tElementA_RealIndex As Long
Dim tElementB_RealIndex As Long
Function DummyActiveArray_ValueGetByIndex(ByRef pArray() As tpActiveArray_Element, ByVal pDummyIndex As Long) As Long
'从ActiveArray获得一个虚拟索引对应的元素值
Dim tOutLong As Long
DummyActiveArray_ValueGetByIndex = tOutLong
End Function
Sub DummyActiveArray_ValuePutToIndex(ByRef pArray() As tpActiveArray_Element, ByVal pDummyIndex As Long, ByVal pValue As Long)
'将一个值写入ActiveArray对应虚拟索引的元素
Function RealActiveArray_IndexGetByDummyIndex(ByRef pArray() As tpActiveArray_Element, ByRef pDummyIndex As Long) As Long
'返回一个虚拟索引对应的元素在一个ActiveArray的真实索引。如果虚拟索引对应的元素不存在,则创建一个。
Dim tOutIndex As Long
Dim tArray_Count As Long
Dim tArray_Index As Long
tOutIndex = -1
tArray_Count = RealActiveArray_Count(pArray())
If CBool(tArray_Count) Then
For tIndex = 0 To tArray_Count - 1
With pArray(tIndex)
If .aaIndex = pDummyIndex Then
tOutIndex = tIndex
Exit For
End If
End With
Next
Else
End If
If tOutIndex < 0 Then
ReDim Preserve pArray(tArray_Count)
With pArray(tArray_Count)
.aaIndex = pDummyIndex
End With
tOutIndex = tArray_Count
End If
RealActiveArray_IndexGetByDummyIndex = tOutIndex
End Function
Function RealActiveArray_Count(ByRef pArray() As tpActiveArray_Element) As Long
'获得一个ActiveArray的真实元素数量。
Dim tOutLong As Long
演示代码:
Private Sub Command1_Click()
Dim tArray() As tpActiveArray_Element
Dim tIndex As Long
For I = 0 To 100
tIndex = Int(Rnd * 2 ^ 16)
DummyActiveArray_ValuePutToIndex tArray(), tIndex, Int(Rnd * 2 ^ 16)
Next
Text1.Text = UBound(tArray)
End Sub
虚拟数组模块:
Type tpActiveArray_Element
aaIndex As Long
aaValue As Long
End Type
Function DummyActiveArray_ValueGetByIndex(ByRef pArray() As tpActiveArray_Element, ByVal pDummyIndex As Long) As Long
'从ActiveArray获得一个虚拟索引对应的元素值
Dim tOutLong As Long
DummyActiveArray_ValueGetByIndex = tOutLong
End Function
Sub DummyActiveArray_ValuePutToIndex(ByRef pArray() As tpActiveArray_Element, ByVal pDummyIndex As Long, ByVal pValue As Long)
'将一个值写入ActiveArray对应虚拟索引的元素
Function RealActiveArray_IndexGetByDummyIndex(ByRef pArray() As tpActiveArray_Element, ByRef pDummyIndex As Long) As Long
'返回一个虚拟索引对应的元素在一个ActiveArray的真实索引。如果虚拟索引对应的元素不存在,则创建一个。
Dim tOutIndex As Long
Dim tArray_Count As Long
Dim tArray_Index As Long
tOutIndex = -1
tArray_Count = RealActiveArray_Count(pArray())
If CBool(tArray_Count) Then
For tIndex = 0 To tArray_Index
With pArray(tIndex)
If .aaIndex = pDummyIndex Then
tOutIndex = tIndex
Exit For
End If
End With
Next
Else
End If
If tOutIndex < 0 Then
ReDim Preserve pArray(tArray_Count)
With pArray(tArray_Count)
.aaIndex = pDummyIndex
End With
tOutIndex = tArray_Count
End If
RealActiveArray_IndexGetByDummyIndex = tOutIndex
End Function
Function RealActiveArray_Count(ByRef pArray() As tpActiveArray_Element) As Long
'获得一个ActiveArray的真实元素数量。
Dim tOutLong As Long
Dim tNumbers() As Long
Dim tMax As Long
Dim tIndex As Long
Dim tSwapIndex As Long
Dim tT As Long
tMax=99
ReDim tNumbers(tMax)
For tIndex=0 To 99
tNumbers(tIndex)=tIndex
Next
For tIndex=0 To 11
tSwapIndex=Int(Rnd*(tMax+1))'确定交换目标
'将当前元素与随机元素交换。
tT=tNumbers(tIndex)
tNumbers(tIndex)=tNumbers(tSwapIndex)
tNumbers(tSwapIndex)=tT
Next
'=============================
Dim i, j As Long
Dim ran(1 To 52) As Long
Dim tNum As Long
Dim isExist As Boolean
Randomize
For i = 1 To 52
isExist = False
tNum = Int(100 * Rnd + 1)
Debug.Print tNum
For j = 1 To i
If ran(j) = tNum Then
isExist = True
i = i - 1
Exit For
End If
Next
If isExist = False Then
ran(i) = tNum
End If
Next
For i = 1 To 52
msg = msg & ran(i) & Space(1)
Next
MsgBox msg