|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- 借用香川裙子老师的代码,改成自定义函数,这样用起来就方便了。
- 权当向各位前辈高手、大神们致敬了
- Function HugeRndNumbers(Optional ByVal Max As Long = 10, Optional ByVal Min As Long = 1, Optional ByVal N As Integer = 1)
- Dim a(), b() As Long
- Dim m, B0, Fd, Cou, i1, i2
- Cou = Max - Min + 1
- ReDim b(N - 1)
- b(0) = Int((2 * Int((Max - b(m) + 1) / (N - m))) * Rnd()) + Min
- Do While m < N - 1
- t = Int((2 * Int((Max - b(m) + 1) / (N - m))) * Rnd()) + 1
- If b(m) + t > Max Then Exit Do
- m = m + 1
- b(m) = b(m - 1) + t
- Loop
- For i = 1 To N - 1 - m
- Do Until t1 > 1
- ct = ct + 1
- If ct > Max Then
- If b(0) - Min > 0 Then
- t = m + 1
- t1 = b(0) - Min
- Else
- For t = 0 To N - 2
- If b(t + 1) - b(t) > 1 Then
- t1 = b(t + 1) - b(t)
- Exit For
- End If
- Next
- End If
- Exit Do
- End If
- t = Int((N - 1) * Rnd())
- t1 = b(t + 1) - b(t)
- Loop
- t2 = Int((t1 - 1) * Rnd()) + 1
- If b(t) + t2 < Min Then
- MsgBox b(t) + t2
- Else
- b(m + 1) = b(t) + t2
- End If
- Next
- For i = N - 1 To 0 Step -1
- t = Int((i + 1) * Rnd())
- t1 = b(t)
- If t1 = 0 Then
- MsgBox t1
- End If
- b(t) = b(i)
- b(i) = t1
- Next
- HugeRndNumbers = b
- End Function
复制代码 |
|