vb数组取值

来源:百度知道 编辑:UC知道 时间:2024/06/28 21:46:54
19个数字,每个都不重复,要求取出4个数,这4个数字都不能重复,最后输出100次结果,要求这100次结果不能有重复的,例如第一次取到1,2,3,4这4个数字,那么第二次就不能取1,2,3,4或是1,2,4,3,或是2,1,3,4等

Private Sub Form_Load()
Dim arr(18) As Integer
Dim num(3) As Integer
Dim i As Integer, j As Integer, k As Integer
Dim dict As New Collection

For i = 0 To 18
arr(i) = i + 1
Next

For i = 0 To 100
Loop2:
For j = 0 To 3
Loop1:
Randomize
Dim tmp As Integer
tmp = arr(Int(Rnd * 19))
For k = 0 To j - 1
If num(k) = tmp Then
GoTo Loop1
End If
Next

num(k) = tmp
Next

SortNum num

If NumExist(dict, num) Then
GoTo Loop2
End If

For j = 0 To 3
Debug.Print num(j) & vbTab;
Next
Debug.Print ""
Next

End Sub

Private Function NumExist(ByRef dict As Collection, num() As Integer) As Boolean
On Error GoTo err_do