如何用VB生成随意不重复的数字

来源:百度知道 编辑:UC知道 时间:2024/05/11 00:54:49
如何让VB实现生成随意不重复数字?比如我要让VB实现生成5位数的随意数字(不多不少只要5位数),怎么编写?并解释下

Private Sub Command2_Click()
Dim a(1 To 5) As Integer, i, j, s As String

For i = 1 To 5
a(i) = Int(10 * Rnd) '生成[0,9]之间的随机数字
For j = 1 To i - 1
If a(i) = a(j) Then i = i - 1: Exit For '将生成的随机数字与已有的随机数字比较,若出现相同的,该数剔除,返回重新生成
Next
Next

For i = 1 To 5
Print a(i);
s = s & a(i)
Next
Print
Print s

End Sub

<%
Dim tt
tt=Split("1,2,3,4,5,6,7,8,9,0",",")
'随机排序
leng=UBound(tt)
randomize
for ii=0 to leng
b=int(rnd()*leng)
temp=tt(b)
tt(b)=tt(ii)
tt(ii)=temp
Next

For i=0 To 4
response.write tt(i)
next
%>

Private Sub Command1_Click()
Dim x(1 To 5) As Integer, s As String
Randomize
x(1) = 9 * Rnd
s = x(1)
For i = 2 To 5
10000: x(i) = 9 * Rnd
For j = 1 To i - 1
If x(j) = x(i) Then GoTo 10000: Exit For
Next
s =