|
补充:如果设定【取值个数】超过数据区间【不重复个数】时:
1. 如果设定了最小间隔=0,那么将会抽取到重复值直至满足取值个数。
2. 如果设定最小间隔>0 (包括h=1时),则会抽取所有不重复值以后,再以0值代替空余的输出位置。最后乱序输出结果。
附代码:- Sub kagawa() '指定区间内 均匀随机取不重复值 按指定列数输出
- Randomize
- d = [d2]: a = [a2] * 10 ^ d: b = [b2] * 10 ^ d
- m = [c2]: h = [e2]: l = [g2]: r = [h2]
-
- ReDim c(m - 1)
- If b - a < m * h Then c(0) = a Else c(0) = Int(((b - a + 1) / m - h) * Rnd()) + a
- n = 0
- Do
- If b - c(n) < (m - n) * h Then
- If c(n) + h <= b Then
- c(n + 1) = c(n) + h
- Else
- GoTo Ext
- End If
- Else
- c(n + 1) = c(n) + Int(((b - c(n) + 1) / (m - n) - h) * Rnd() * 2) + h
- End If
- n = n + 1
- Loop Until n = m - 2
- c(m - 1) = c(n) + Int((b - c(n) + 1 - h) * Rnd()) + h
- Ext:
- ReDim f(m \ l, l - 1)
- If r = 0 Then
- For i = 0 To m - 1
- r = Int((m - i) * Rnd()) + i
- t = c(r): c(r) = c(i): c(i) = c(r)
- f(i \ l, i Mod l) = t / 10 ^ d
- Next
- Else
- For i = 0 To m - 1
- f(i \ l, i Mod l) = c(i) / 10 ^ d
- Next
- End If
- [a4].CurrentRegion.Offset(1) = ""
- [a5].Resize(m \ l + 1, l) = f
-
- End Sub
复制代码 |
|