Generate a Random String

Description: Generates a random string of characters of "num" length.
Tested Platform: .NET 4.6, Visual Studio 2015, Windows 7/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))
        Next

        Return generate_str.ToString()
End Function

Submitted: September 01, 2012

Return to the snippets listing