'作者:星空无月(xkwy)
Private Sub Command1_Click()
Dim i As Integer, k As Integer
Dim strText As String
Dim a(9) As Integer
For i = 0 To 9
Do While Repeat(a(), k, i) '如果重复的话继续获取
k = Int(Rnd * 50 + 1) '获取1~50的整数
Loop
a(i) = k
Next
For i = 0 To 9
strText = strText & " " & a(i)
Next
Label1.AutoSize = True
Label1.Caption = strText
End Sub
Private Function Repeat(a() As Integer, Num As Integer, Index As Integer) As Boolean
'参数:a()表示要判断的数组,Num表示被重复的数字,index表示判断到几号就不看了
Dim j As Integer
For j = LBound(a) To Index '如果有重复则返回True
If Num = a(j) Then Repeat = True: Exit Function
Next
Repeat = False
End Function
创建一个数组,A(50),把数字1~50,存在相应的数组中,让后根据随机数的值,交换对应下标的数组存放的值,最后把数组的前10个数保存,或是输出即可,这样就不会出现重复值,也可简化算法。