gpt4 book ai didi

vb.net - 生成随机字母数字字符串

转载 作者:行者123 更新时间:2023-12-04 23:57:35 25 4
gpt4 key购买 nike

我想像这样在 vb.net 中生成一个随机代码

  Dim r As New Random
Response.Write(r.Next())

但我想生成 6 位数字的代码,并且应该是字母数字,如 thie A12RV1所有代码都应该是这样的。

我已经尝试过 vb.net 随机类(class),但我无法像我想要的那样做。每次执行代码时,我都想获取字母数字代码。我怎样才能在 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

关于vb.net - 生成随机字母数字字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15288692/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com