我考虑用个辅助过程,加快其速度
程序代码: | [ 复制代码 ] |
Function RandABC(N As Long, Optional Model As Long = 1) As String
''N是你想要得到字符串的长度
''Model是你想得到大写还是小写字母,1--混合大小写,2--大写,3--小写"
Dim StrDiaoyong As String
Const StrHunhe = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
Const StrDaxie = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Const StrXiaoxie = "abcdefghijklmnopqrstuvwxyz"
Select Case Model
Case 1
StrDiaoyong = StrHunhe
Case 2
StrDiaoyong = StrDaxie
Case 3
StrDiaoyong = StrXiaoxie
End Select
If N <= 20000 Then
For i = 1 To N
Randomize
RandABC = RandABC & Mid(StrDiaoyong, Int(Rnd * (Len(StrDiaoyong) - 1 + 1) + 1), "1")
Next i
Else
MsgBox "N值太大了,计算机缓冲不过来"
End If
End Function
''N是你想要得到字符串的长度
''Model是你想得到大写还是小写字母,1--混合大小写,2--大写,3--小写"
Dim StrDiaoyong As String
Const StrHunhe = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
Const StrDaxie = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Const StrXiaoxie = "abcdefghijklmnopqrstuvwxyz"
Select Case Model
Case 1
StrDiaoyong = StrHunhe
Case 2
StrDiaoyong = StrDaxie
Case 3
StrDiaoyong = StrXiaoxie
End Select
If N <= 20000 Then
For i = 1 To N
Randomize
RandABC = RandABC & Mid(StrDiaoyong, Int(Rnd * (Len(StrDiaoyong) - 1 + 1) + 1), "1")
Next i
Else
MsgBox "N值太大了,计算机缓冲不过来"
End If
End Function
以下来自iceboy的
程序代码: | [ 复制代码 ] |
Public Function RandomStr(ByVal Length As Long) As String
Const RandomChar As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
Dim RandomBytes() As Byte, RandomCharCount As Long, Result() As Byte, i As Long, p As Long
If Length <> 0 Then
Randomize
RandomBytes = RandomChar
RandomCharCount = UBound(RandomBytes) \ 2 + 1
ReDim Result(Length * 2 - 1)
For i = 0 To Length - 1
p = Int(Rnd() * RandomCharCount)
Result(i * 2) = RandomBytes(p * 2)
Result(i * 2 + 1) = RandomBytes(p * 2 + 1)
Next
RandomStr = Result
End If
End Function
Const RandomChar As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
Dim RandomBytes() As Byte, RandomCharCount As Long, Result() As Byte, i As Long, p As Long
If Length <> 0 Then
Randomize
RandomBytes = RandomChar
RandomCharCount = UBound(RandomBytes) \ 2 + 1
ReDim Result(Length * 2 - 1)
For i = 0 To Length - 1
p = Int(Rnd() * RandomCharCount)
Result(i * 2) = RandomBytes(p * 2)
Result(i * 2 + 1) = RandomBytes(p * 2 + 1)
Next
RandomStr = Result
End If
End Function
相关链接:vb | 随机字母 | 函数 | 过程
[本日志由 admin 于 2013-11-01 10:46 PM 编辑]
引用通告地址 (0):
http://www.cnng.net/blog/trackback.asp?tbID=23
http://www.cnng.net/blog/trackback.asp?tbID=23&CP=GBK
http://www.cnng.net/blog/trackback.asp?tbID=23
http://www.cnng.net/blog/trackback.asp?tbID=23&CP=GBK