我试图像这样在vb.net中生成随机代码 Dim r As New Random Response.Write(r.Next()) 但我想生成6位数的代码,并且应该像A12RV1那样是字母数字,所有代码应该是这样的. 我已经尝试过vb.net随机类,但我无
Dim r As New Random Response.Write(r.Next())
但我想生成6位数的代码,并且应该像A12RV1那样是字母数字,所有代码应该是这样的.
我已经尝试过vb.net随机类,但我无法像我想的那样做.我希望每次执行代码时都能获得字母数字代码.我怎样才能在vb.net中实现这一目标?
尝试这样的事情:Public Function GetRandomString(ByVal iLength As Integer) As String Dim sResult As String = "" Dim rdm As New Random() For i As Integer = 1 To iLength sResult &= ChrW(rdm.Next(32, 126)) Next Return sResult End Function
或者您可以使用定义有效字符的常用随机字符串:
Public Function GenerateRandomString(ByRef iLength As Integer) As String Dim rdm As New Random() Dim allowChrs() As Char = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ0123456789".ToCharArray() Dim sResult As String = "" For i As Integer = 0 To iLength - 1 sResult += allowChrs(rdm.Next(0, allowChrs.Length)) Next Return sResult End Function