Generate a Random String

Description: Generates a random string of characters of "num" length.
Tested Platform: .NET 4.8, Visual Studio 2022, Windows 10
Language: VB.NET
' Generate a string of random letters/numbers totally the length of "num"
Private Function GenerateString(num As Integer) As String
    If num < 1 Then
        num = 1
    End If

    Dim str As String = "abcdefghijklmnopqrstuvwxyz1234567890"
    Dim generate_str As New System.Text.StringBuilder()
    Dim random As New Random()

    For i As Integer = 1 To num
        generate_str.Append(str.Substring(random.Next(0, str.Length), 1))

    Return generate_str.ToString()
End Function

Posted: March 20, 2023

Return to the snippets listing