7

好吧,我知道有很多这些线程,但我是 vb.net 的新手,但我无法编辑给出的源代码来制作我真正想要的东西,所以我想要一个函数,它会生成随机字符串,每个字符串包含 15-32 个字符并且它们每个都将具有以下字符(不是所有字符都在同一个字符串中,而是其中一些字符):AZ az 0-9 这是我到目前为止的代码

Functon RandomString()
    Dim s As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
    Dim r As New Random
    Dim sb As New StringBuilder
    For i As Integer = 1 To 8
        Dim idx As Integer = r.Next(0, 35)
        sb.Append(s.Substring(idx, 1))
    Next
    return sb.ToString()
End Function
4

8 回答 8

21

更改字符串以包含 az 字符:

Dim s As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"

更改循环以创建随机数量的字符:

Dim cnt As Integer = r.Next(15, 33)
For i As Integer = 1 To cnt

请注意,方法中的上限Next是互斥的,因此Next(15, 33)给您一个范围为 15 到 32 的值。

使用字符串的长度从中选择一个字符:

Dim idx As Integer = r.Next(0, s.Length)

由于您要创建随机字符串,而不是单个随机字符串,因此不应在函数内部创建随机数生成器。如果您两次调用该函数的时间太接近,您最终会得到相同的随机字符串,因为随机生成器是使用系统时钟播种的。因此,您应该将随机生成器发送到函数中:

Function RandomString(r As Random)

所以,总而言之:

Function RandomString(r As Random)
  Dim s As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
  Dim sb As New StringBuilder
  Dim cnt As Integer = r.Next(15, 33)
  For i As Integer = 1 To cnt
    Dim idx As Integer = r.Next(0, s.Length)
    sb.Append(s.Substring(idx, 1))
  Next
  return sb.ToString()
End Function

使用示例:

Dim r As New Random
Dim strings As New List<string>()
For i As Integer = 1 To 10
  strings.Add(RandomString(r))
Next
于 2013-03-18T19:18:48.547 回答
3

尝试这样的事情: -

stringToReturn&= Guid.NewGuid.ToString().replace("-","")

你也可以检查这个: -

Sub Main()
        Dim KeyGen As RandomKeyGenerator
        Dim NumKeys As Integer
        Dim i_Keys As Integer
        Dim RandomKey As String

        ''' MODIFY THIS TO GET MORE KEYS    - LAITH - 27/07/2005 22:48:30 -
        NumKeys = 20

        KeyGen = New RandomKeyGenerator
        KeyGen.KeyLetters = "abcdefghijklmnopqrstuvwxyz"
        KeyGen.KeyNumbers = "0123456789"
        KeyGen.KeyChars = 12
        For i_Keys = 1 To NumKeys
            RandomKey = KeyGen.Generate()
            Console.WriteLine(RandomKey)
        Next
        Console.WriteLine("Press any key to exit...")
        Console.Read()
    End Sub
于 2013-03-18T19:02:18.740 回答
2

以您的功能为指导,我将其修改为:

  1. 随机化长度(在 minChar 和 maxCharacters 之间)
  2. 随机化每次生成的字符串(使用静态随机)

代码:

Function RandomString(minCharacters As Integer, maxCharacters As Integer)
    Dim s As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
    Static r As New Random
    Dim chactersInString As Integer = r.Next(minCharacters, maxCharacters)
    Dim sb As New StringBuilder
    For i As Integer = 1 To chactersInString
        Dim idx As Integer = r.Next(0, s.Length)
        sb.Append(s.Substring(idx, 1))
    Next
    Return sb.ToString()
End Function
于 2013-03-18T19:16:12.513 回答
2

试试这个:

Private Function RandomString(ByRef Length As String) As String
    Dim str As String = Nothing
    Dim rnd As New Random
    For i As Integer = 0 To Length
        Dim chrInt As Integer = 0
        Do
            chrInt = rnd.Next(30, 122)
            If (chrInt >= 48 And chrInt <= 57) Or (chrInt >= 65 And chrInt <= 90) Or (chrInt >= 97 And chrInt <= 122) Then
                Exit Do
            End If
        Loop
        str &= Chr(chrInt)
    Next
    Return str
End Function
于 2016-05-08T11:00:21.310 回答
0

您需要将线路更改For i As Integer = 1 To 8For i As Integer = 1 To ?哪里?是字符串应该是长的字符数。这会更改重复以下代码的次数,以便将更多字符附加到字符串中。

    Dim idx As Integer = r.Next(0, 35)
    sb.Append(s.Substring(idx, 1))
于 2013-03-18T19:06:28.980 回答
0

我的 $.02

Dim prng As New Random
Const minCH As Integer = 15 'minimum chars in random string
Const maxCH As Integer = 35 'maximum chars in random string

'valid chars in random string
Const randCH As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"

Private Function RandomString() As String
    Dim sb As New System.Text.StringBuilder
    For i As Integer = 1 To prng.Next(minCH, maxCH + 1)
        sb.Append(randCH.Substring(prng.Next(0, randCH.Length), 1))
    Next
    Return sb.ToString()
End Function
于 2013-03-18T19:41:29.290 回答
0

请注意,

r.Next(0, 35)

倾向于挂起并显示相同的结果不知道为什么;更好用

CInt(Math.Ceiling(Rnd() * N)) + 1

在这里看到它VB.NET 中的随机整数

于 2015-02-07T18:55:56.420 回答
0

我根据自己的需要加强了 Nathan Koop 的功能,并认为我会分享。

我补充说:

  • 能够将前置和附加文本添加到随机字符串
  • 能够选择允许的字符(字母)的大小写
  • 能够选择在允许的字符中包含/排除数字

注意:如果严格寻找确切长度的字符串,同时还添加预/附加字符串,您需要处理它;我遗漏了任何逻辑来处理它。

示例用法:

' Straight call for a random string of 20 characters
' All Caps + Numbers
String_Random(20, 20, String.Empty, String.Empty, 1, True)

' Call for a 30 char string with prepended string
' Lowercase, no numbers
String_Random(30, 30, "Hey_Now_", String.Empty, 2, False)

' Call for a 15 char string with appended string
' Case insensitive + Numbers
String_Random(15, 15, String.Empty, "_howdy", 3, True)

.

Public Function String_Random(
    intMinLength As Integer,
    intMaxLength As Integer,
    strPrepend As String,
    strAppend As String,
    intCase As Integer,
    bIncludeDigits As Boolean) As String

    ' Allowed characters variable
    Dim s As String = String.Empty

    ' Set the variable to user's choice of allowed characters
    Select Case intCase

        Case 1

            ' Uppercase
            s  = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

        Case 2

            ' Lowercase
            s = "abcdefghijklmnopqrstuvwxyz"                

        Case Else

            ' Case Insensitive + Numbers
            s  = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" 

    End Select

    ' Add numbers to the allowed characters if user chose so
    If bIncludeDigits = True Then s &= "0123456789"

    Static r As New Random

    Dim chactersInString As Integer = r.Next(intMinLength, intMaxLength)
    Dim sb As New StringBuilder

    ' Add the prepend string if one was passed
    If String.IsNullOrEmpty(strPrepend) = False Then sb.Append(strPrepend)

    For i As Integer = 1 To chactersInString

        Dim idx As Integer = r.Next(0, s.Length)

        sb.Append(s.Substring(idx, 1))

    Next

    ' Add the append string if one was passed
    If String.IsNullOrEmpty(strAppend) = False Then sb.Append(strAppend)

    Return sb.ToString()

End Function
于 2019-05-10T02:39:56.000 回答