27
Mar
09

[VB.Net] RandomString Generator II

An updated version of my random string generator.
This version is more versatile than the last, allowing for more flexibility in the output.

'''
''' A function to generate random strings
'''
''' The length of the returned string.
''' Define a custom set of characters that are possible to be chosen
''' Determines if the string should contain Alphabetical Characters. Default = True
''' Determines if the string should contain Numerical Characters. Default = True
''' Determines if the string should contain Symbols. Default = False
''' Determines if the string should contain Upper Case characters
''' Determines if the string should contain Lower Case characters
''' A random string of the desired length.
''' http://simon.ctranter.co.uk
Private Function RandomString(ByVal StringLength As Integer, Optional ByVal CustomCharacters As String = "", Optional ByVal Letters As Boolean = True, Optional ByVal Numbers As Boolean = True, Optional ByVal Symbols As Boolean = False, Optional ByVal UpperCase As Boolean = True, Optional ByVal LowerCase As Boolean = True) As String
    Dim randomG As New Random
    Dim randomN As Integer
    Const pwChars As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    Const pwNumbers As String = "0123456789"
    Const pwSymbols As String = "`¬!""£$%^&*()_)+-=[]{};':@#~/?.>,<|"""
    Dim str As String = ""
    While str.Length < StringLength
        Dim pwcharList As String = ""
        If Letters = True Then pwcharList &= pwChars
        If Numbers = True Then pwcharList &= pwNumbers
        If Symbols = True Then pwcharList &= pwSymbols
        pwcharList &= CustomCharacters
        randomN = randomG.Next(0, pwcharList.Length)
        If UpperCase = True And LowerCase = True Then
            Dim randomN2 = randomG.Next(0, 2)
            If randomN2 = 0 Then
                str &= Char.ToUpper(pwcharList(randomN), System.Globalization.CultureInfo.CurrentCulture)
            Else
                str &= Char.ToLower(pwcharList(randomN), System.Globalization.CultureInfo.CurrentCulture)
            End If
        ElseIf UpperCase = True Then
            str &= Char.ToUpper(pwcharList(randomN), System.Globalization.CultureInfo.CurrentCulture)
        ElseIf LowerCase = True Then
            str &= Char.ToLower(pwcharList(randomN), System.Globalization.CultureInfo.CurrentCulture)
        Else
            str &= pwcharList(randomN)
        End If
    End While
    Return str
End Function

As you can see, this version is slightly more complicated than the last, but as stated, it also allows for far more flexibility in the output strings.
Ill go over the function slightly:

Private Function RandomString( _
ByVal StringLength As Integer, _
    Optional ByVal CustomCharacters As String = "", _
    Optional ByVal Letters As Boolean = True, _
    Optional ByVal Numbers As Boolean = True, _
    Optional ByVal Symbols As Boolean = False, _
    Optional ByVal UpperCase As Boolean = True, _
    Optional ByVal LowerCase As Boolean = True) _
As String

The key thing to note about this version, is the introduction of the CustomCharacters paramater. This allows you to make random strings, but using your own predefined arrays of characters.

For example, if i was to use this:

Debug.WriteLine(RandomString(20, "simon.ctranter.co.uk", False, False, False))

It would output something like this:
t.scCKcrn.CN.o..A.Ms
As to make the string, it only uses characters from the CustomCharacters parameter.

So, what use is this?
It allows you to make “random strings” that, are more recognisable, for example, using:

Debug.WriteLine(RandomString(40, "simonsimonsimonsimonsimonsimon", False, False, False, True, True))

You would get:
ISnoNnSsNinSioommOSNNSsMoSiomsOoSSIiMsIO

Which could then be made more complex by allowing symbols, but also increasing the length of the CustomCharacters (So it is still more likley to pick on of the characters from ‘simon’):

Debug.WriteLine(RandomString(40, "simonsimonsimonsimonsimonsimonsimonsimonsimonsimonsimonsimon", False, False, True, True, True))

Would return:
.~&)>M|mS}SNIsn[iiNSoonOSSM”Nmi*O|_(S)i

Advertisements

0 Responses to “[VB.Net] RandomString Generator II”



  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: