Option Explicit
Dim max As Integer, min As Integer
Private Sub Command1_Click()
Dim a(6) As Integer
Dim i As Integer
min = CInt(Val(Text1))
max = CInt(Val(Text2))
If max < min Or (max - min) < 7 Then
MsgBox "error"
Exit Sub
End If
Randomize
For i = 0 To 6
a(i) = CInt(min + Rnd * (max - min))
If equal(a(), i) Then
MsgBox "i=" & i & "
" & a(i)
a(i) = notEqual(a(), i)
End If
Next
For i = 0 To 6
Debug.Print a(i)
Next
End Sub
Private Function equal(a() As Integer, pos As Integer) As Boolean
Dim i As Integer
For i = 0 To pos
If a(pos) = a(i) And pos <> i Then
equal = True
Exit Function
End If
Next
equal = False
End Function
Private Function notEqual(a() As Integer, pos As Integer) As Integer
Dim i As Integer, j As Integer, b(100) As Integer
For i = 0 To CInt(Val(max - min))
b(i) = CInt(min) + i
Next
For i = 0 To pos - 1
For j = 0 To CInt(Val(max - min))
If a(i) = b(j) Then
b(j) = CInt(min) - 1
End If
Next
Next
For i = 0 To CInt(Val(max - min))
If b(i) > CInt(min) Then
notEqual = b(i)
End If
Next
End Function
程序比较幼稚,冗余,望高手不要见笑