用VB产生几个不同的随机数

来源:百度知道 编辑:UC知道 时间:2024/06/18 07:29:09
我要辨写一个自动考试系统
aaaaaa,aaaaab,aaaaac,aaaaad到aaaaao为1,2,3到10000之间的不同的随机数

Private Sub Form_Load()
a = GetRndNotRepeat(1, 10000, 2000)
For i = 0 To 1999
Debug.Print a(i)
Next i
End Sub

Public Function GetRndNotRepeat(ByVal NumMin As Integer, ByVal NumMax As Integer, ByVal n As Integer)
'编制:xsfhlzh
'功能:取NumMin到NumMax间的n个随机整数
If n > NumMax - NumMin + 1 Then Exit Function
Dim arr()
ReDim arr(n - 1)
Dim b() As Boolean
ReDim b(NumMax - NumMin) '取数标志
Dim x As Integer, y As Integer
Randomize
For i = 0 To n - 1
Do '找到x的位置,y表示x在取数标志数组的位置
x = Int(Rnd * (NumMax - NumMin + 1)) + NumMin
y = x - NumMin
Loop While b(y)
b(y) = True
arr(i) = x '找到未取的数,并放入数组,设置标志位
Next i
GetRndNotRepeat = arr
End Function