科班做法 :-)Option Explicit Option Base 0Private Sub Command1_Click() Dim a(19) Dim i As Integer Dim x As Integer Dim j As Integer
For i = 0 To 19 a(i) = i Next i
For i = 19 To 10 Step -1 Randomize x = Int((i + 1) * Rnd) Debug.Print a(x) + 1, For j = x To i - 1 a(j) = a(j + 1) Next j Next i
Debug.Print End Sub
dim a as new collection dim i as integer dim b as integer for i = 1 to 20 a.add i next for i=1 to 10 randomize b=int(rnd()*a.count)+1 debug.print a(b) a.delete b next
Dim a(10) For i = 1 To 10 xx: Randomize a(i) = Int(Rnd() * 20 + 1) For j = 1 To i - 1 If a(j) = a(i) Then GoTo xx Next Debug.Print a(i)
Next
用集合要简单一些:Private Sub Form_Load() Dim temp As New Collection Dim a(9) As String, num As Integer For i = 1 To 20 temp.Add i Next Randomize For i = 0 To 9 num = Int(Rnd * temp.Count + 1) a(i) = temp(num) temp.Remove num Next Set temp = Nothing MsgBox Join(a(), ","), 64, "10 random number between 1 to 20" End Sub
Dim a(20)
Dim i As Integer
Dim x As Integer
Dim j As Integer
For i = 0 To 19
a(i) = i
Next i
For i = 19 To 10 Step -1
Randomize
x = Int((i + 1) * Rnd)
Debug.Print a(x) + 1,
For j = x To i - 1
a(j) = a(j + 1)
Next j
Next i
Debug.Print
End Sub
这位仁兄的数组为什么空个a(20)不用啊?
我想一定不是VB科班出身的。程序基本上是对的,取10至19不重复的数。
Option Base 0Private Sub Command1_Click()
Dim a(19)
Dim i As Integer
Dim x As Integer
Dim j As Integer
For i = 0 To 19
a(i) = i
Next i
For i = 19 To 10 Step -1
Randomize
x = Int((i + 1) * Rnd)
Debug.Print a(x) + 1,
For j = x To i - 1
a(j) = a(j + 1)
Next j
Next i
Debug.Print
End Sub
dim i as integer
dim b as integer
for i = 1 to 20
a.add i
next
for i=1 to 10
randomize
b=int(rnd()*a.count)+1
debug.print a(b)
a.delete b
next
For i = 1 To 10
xx:
Randomize
a(i) = Int(Rnd() * 20 + 1)
For j = 1 To i - 1
If a(j) = a(i) Then GoTo xx
Next
Debug.Print a(i)
Next
Dim temp As New Collection
Dim a(9) As String, num As Integer
For i = 1 To 20
temp.Add i
Next
Randomize
For i = 0 To 9
num = Int(Rnd * temp.Count + 1)
a(i) = temp(num)
temp.Remove num
Next
Set temp = Nothing
MsgBox Join(a(), ","), 64, "10 random number between 1 to 20"
End Sub