Results 1 to 10 of 10

Thread: Generate Random String (Letters & Numbers)

Threaded View

  1. #1

    Thread Starter
    Fanatic Member Emcrank's Avatar
    Join Date
    Jan 2009
    Posts
    566

    Generate Random String (Letters & Numbers)

    Here is a Sub to Generate a random string out of the letters A-Z and numbers 0-9, you can add more if you like.
    ADDED: Feature so you cannot get the same string twice. (Even though it is very unlikly)
    VB.NET Code:
    1. Private Sub GenerateString(ByVal Control As Control)
    2.         Dim ListChars As New List(Of String)({"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", _
    3.                               "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", _
    4.                               "u", "v", "w", "x", "y", "z", _
    5.                               "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", _
    6.                               "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", _
    7.                               "U", "V", "W", "X", "Y", "Z", _
    8.                               "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"})
    9.         Dim RandNumb As New Random
    10.         Dim random1 As String = ListChars(RandNumb.Next(0, ListChars.Count - 1))
    11.         Dim random2 As String = ListChars(RandNumb.Next(0, ListChars.Count - 1))
    12.         Dim random3 As String = ListChars(RandNumb.Next(0, ListChars.Count - 1))
    13.         Dim random4 As String = ListChars(RandNumb.Next(0, ListChars.Count - 1))
    14.         Dim random5 As String = ListChars(RandNumb.Next(0, ListChars.Count - 1))
    15.         Dim random6 As String = ListChars(RandNumb.Next(0, ListChars.Count - 1))
    16.         Dim random7 As String = ListChars(RandNumb.Next(0, ListChars.Count - 1))
    17.         Dim random8 As String = ListChars(RandNumb.Next(0, ListChars.Count - 1))
    18.         Dim History As New List(Of String)
    19.         Dim GeneratedString As String = random1 & random2 & random3 & random4 & random5 & random6 & random7 & random8
    20.         If History.Contains(GeneratedString) Then
    21.             Do Until Not History.Contains(GeneratedString)
    22.                 random1 = ListChars(RandNumb.Next(0, ListChars.Count - 1))
    23.                 random2 = ListChars(RandNumb.Next(0, ListChars.Count - 1))
    24.                 random3 = ListChars(RandNumb.Next(0, ListChars.Count - 1))
    25.                 random4 = ListChars(RandNumb.Next(0, ListChars.Count - 1))
    26.                 random5 = ListChars(RandNumb.Next(0, ListChars.Count - 1))
    27.                 random6 = ListChars(RandNumb.Next(0, ListChars.Count - 1))
    28.                 random7 = ListChars(RandNumb.Next(0, ListChars.Count - 1))
    29.                 random8 = ListChars(RandNumb.Next(0, ListChars.Count - 1))
    30.                 GeneratedString = random1 & random2 & random3 & random4 & random5 & random6 & random7 & random8
    31.             Loop
    32.         End If
    33.         Control.Text = GeneratedString
    34.     End Sub
    And to call this sub to put the generated string in a textbox just do,
    VB.NET Code:
    1. GenerateString(TextBox1)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width