''生成一组随机数序列,可以指定重复率 [ 参数: 待填充数组, 数据数量, 重复率% ]
Sub RanSequence(Result() As Long, ByVal ItemCount As Long, Optional ByVal Repetition As Byte = 0)
If ItemCount < 2 Or Repetition > 100 Then Exit Sub
ReDim Result(0 To ItemCount - 1): If UBound(Result) < 0 Then Exit Sub
Dim i As Long, k As Long
k = Fix(((100# - Repetition) / 100#) * ItemCount) '不重复的数量
For i = 0 To k - 1: Result(i) = i: Next '不重复的原始数据
For i = k To ItemCount - 1 '附加重复的原始数据(如果有重复率)
Result(i) = Fix(k * Rnd)
Next
Randomize '把数组打乱顺序
For i = ItemCount - 1 To 1 Step -1
k = Fix(i * Rnd)
Result(i) = Result(i) Xor Result(k)
Result(k) = Result(i) Xor Result(k)
Result(i) = Result(i) Xor Result(k)
Next
End Sub
Private Sub Command1_Click()
Dim a As Long, b As Long: a = 2: b = 5000 '概率区间 [a, b]
Dim c As Long: c = (b - a) + 1 '区间长度 c = (b - a) + 1
Dim n As Long: n = 100 '数据总量 n = 100
Dim p As Long: p = 20 '重复率 p = 20 (%)
Dim w As Long: w = Fix((100# - p) / 100# * n) '不重复的数量
If c < w Then MsgBox "概率区间不够" : Exit Sub
Dim x() As Long, y() As Long
ReDim x(0 To c - 1): If UBound(x) = -1 Then MsgBox "内存不足": Exit Sub
ReDim y(0 To n - 1): If UBound(y) = -1 Then MsgBox "内存不足": Exit Sub
''------生成一组随机数序列,可以指定重复率----------
Dim i As Long, k As Long: Randomize
For i = 0 To c - 1: x(i) = a + i: Next '填充概率区间
For i = c - 1 To 1 Step -1
k = Fix(Rnd * i) '概率区间打乱顺序
x(i) = x(i) Xor x(k): x(k) = x(i) Xor x(k): x(i) = x(i) Xor x(k)
Next
For i = 0 To w - 1: y(i) = x(i): Next '填充不重复的部分
For i = w To n - 1
k = Fix(Rnd * w): y(i) = y(k) '从不重复的部分随机取数,造成重复
Next
For i = n - 1 To 1 Step -1
k = Fix(Rnd * i) '再1次打乱顺序
y(i) = y(i) Xor y(k): y(k) = y(i) Xor y(k): y(i) = y(i) Xor y(k)
Next
''-------验证重复率--------------------------------
w = 0
For i = 0 To n - 1
If y(i) <> (a - 1) Then
w = w + 1
For k = i + 1 To n - 1
If y(k) = y(i) Then y(k) = (a - 1)
Next
End If
Next
Debug.Print "不重复的数有 " & Str(w) & " 个",
Debug.Print "重复率 = " & Str(Fix(CDbl(n - w) / n * 100#)) & "%"
End Sub