Click here to Skip to main content
15,896,063 members
Articles / Programming Languages / Visual Basic

Generate a Random String Key in VB.NET

Rate me:
Please Sign up or sign in to vote.
4.56/5 (22 votes)
27 Jul 20052 min read 186.9K   3.7K   31  
A slightly more intelligent random string generator.
Option Strict On
Imports System.Text
''' <date>27072005</date><time>070339</time>
''' <type>class</type>
''' <summary>
''' REQUIRES PROPERTIES: KeyLetters, KeyNumbers, MaxChars
''' </summary>
Public Class RandomKeyGenerator
    Dim Key_Letters As String
    Dim Key_Numbers As String
    Dim Key_Chars As Integer
    Dim LettersArray As Char()
    Dim NumbersArray As Char()

    ''' <date>27072005</date><time>071924</time>
    ''' <type>property</type>
    ''' <summary>
    ''' WRITE ONLY PROPERTY. HAS TO BE SET BEFORE CALLING GENERATE()
    ''' </summary>
    Protected Friend WriteOnly Property KeyLetters() As String
        Set(ByVal Value As String)
            Key_Letters = Value
        End Set
    End Property
    ''' <date>27072005</date><time>071924</time>
    ''' <type>property</type>
    ''' <summary>
    ''' WRITE ONLY PROPERTY. HAS TO BE SET BEFORE CALLING GENERATE()
    ''' </summary>
    Protected Friend WriteOnly Property KeyNumbers() As String
        Set(ByVal Value As String)
            Key_Numbers = Value
        End Set
    End Property
    ''' <date>27072005</date><time>071924</time>
    ''' <type>property</type>
    ''' <summary>
    ''' WRITE ONLY PROPERTY. HAS TO BE SET BEFORE CALLING GENERATE()
    ''' </summary>
    Protected Friend WriteOnly Property KeyChars() As Integer
        Set(ByVal Value As Integer)
            Key_Chars = Value
        End Set
    End Property

    ''' <date>27072005</date><time>072344</time>
    ''' <type>function</type>
    ''' <summary>
    ''' GENERATES A RANDOM STRING OF LETTERS AND NUMBERS. LETTERS CAN BE RANDOMLY CAPITAL OR SMALL.
    ''' </summary>
    ''' <returns type="String">RETURNS THE RANDOMLY GENERATED KEY</returns>
    Function Generate() As String
        Dim i_key As Integer
        Dim Random1 As Single
        Dim arrIndex As Int16
        Dim sb As New StringBuilder
        Dim RandomLetter As String

        ''' CONVERT LettersArray & NumbersArray TO CHARACTR ARRAYS
        LettersArray = Key_Letters.ToCharArray
        NumbersArray = Key_Numbers.ToCharArray

        For i_key = 1 To Key_Chars
            ''' START THE CLOCK    - LAITH - 27/07/2005 18:01:18 -
            Randomize()
            Random1 = Rnd()
            arrIndex = -1
            ''' IF THE VALUE IS AN EVEN NUMBER WE GENERATE A LETTER, OTHERWISE WE GENERATE A NUMBER   - LAITH - 27/07/2005 18:02:55 -
            ''' THE NUMBER '111' WAS RANDOMLY CHOSEN. ANY NUMBER WILL DO, WE JUST NEED TO BRING THE VALUE ABOVE '0'     - LAITH - 27/07/2005 18:40:48 -
            If (CType(Random1 * 111, Integer)) Mod 2 = 0 Then
                ''' GENERATE A RANDOM LOCATION  IN THE LETTERS CHARACTER ARRAY   - LAITH - 27/07/2005 18:47:44 -
                Do While arrIndex < 0
                    arrIndex = Convert.ToInt16(LettersArray.GetUpperBound(0) * Random1)
                Loop
                RandomLetter = LettersArray(arrIndex)
                ''' CREATE ANOTHER RANDOM NUMBER. IF IT IS ODD, WE CAPITALIZE THE LETTER    - LAITH - 27/07/2005 18:55:59 -
                If (CType(arrIndex * Random1 * 99, Integer)) Mod 2 <> 0 Then
                    RandomLetter = LettersArray(arrIndex).ToString
                    RandomLetter = RandomLetter.ToUpper
                End If
                sb.Append(RandomLetter)
            Else
                ''' GENERATE A RANDOM LOCATION  IN THE NUMBERS CHARACTER ARRAY   - LAITH - 27/07/2005 18:47:44 -
                Do While arrIndex < 0
                    arrIndex = Convert.ToInt16(NumbersArray.GetUpperBound(0) * Random1)
                Loop
                sb.Append(NumbersArray(arrIndex))
            End If
        Next
        Return sb.ToString
    End Function
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions