vb.net Code:
Imports System.Text
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim str As New RandomStringMaker
Dim listToAdd As New List(Of String)({"⌐", "½", "¢"}) 'note that you can add "symbols" that are more than one character long, be careful doing this
'because it might mess with the length of the string, and I havn't tested it throughly
Dim listToRemove As New List(Of String)({"A", "z"}) 'removes "A" and "z" from the list of characters.
str.EnableSymbols() ' enables the 30 default symbols
str.RemoveCharacters(listToRemove.ToList) ' removes A and z from the list of characters we build our string from.
str.AddCharacters(listToAdd.ToList) ' adds ¢, ½, ⌐ to the list of characters we build our string from.
ListBox1.Items.Add(str.GenerateString(5)) ' creates a string 5 characters long.
str.DisableSymbols() 'removes the default 30 symbols, it is NOT neccessary to call this, and how i have it now is USELESS, but just incase ya needed the example it is here.
End Sub
End Class
Public Class RandomStringMaker
'30 symbols that are acceptable in passwords according to this site: http://publib.boulder.ibm.com/infocenter/tivihelp/v4r1/index.jsp?topic=/com.ibm.tpc_V33.doc/fqz0_r_valid_userids_and_passwords.html
Dim _Symbols As New List(Of String)({"`", "~", "@", "#", "%", "^", "&", "*", "(", ")", _
"-", "_", "=", "+", "[", "]", "{", "}", "\", "|", _
";", ":", "'", """", ",", ".", "<", ">", "/", "?"})
'list of A-Z,a-z, 0-9 / characters we will use to build our string.
Dim ListChars As New List(Of String)({"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", _
"k", "l", "m", "n", "o", "p", "q", "r", "s", "t", _
"u", "v", "w", "x", "y", "z", _
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", _
"K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", _
"U", "V", "W", "X", "Y", "Z", _
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"})
Dim History As New List(Of String) 'holds all the strings we generate so we do not get a double.
'leaving the next 2 subs public just incase you want to add your own symbols.
'remove certain characters from the list we use to build our strings
Public Sub RemoveCharacters(ByVal characters As List(Of String))
For i As Integer = 0 To characters.Count - 1
ListChars.Remove(characters(i).ToString)
Next
End Sub
'add certain characters from the list we use to build our strings
Public Sub AddCharacters(ByVal characters As List(Of String))
ListChars.AddRange(characters.ToList)
End Sub
'adds the 30 acceptable symbols to the list of characters with which we will build our string from.
Public Sub EnableSymbols()
AddCharacters(_Symbols.ToList)
End Sub
'removes the 30 acceptable symbols from the list of the characters used to build our strings.
Public Sub DisableSymbols()
RemoveCharacters(_Symbols.ToList)
End Sub
Public Function GenerateString(ByVal length As Integer) As String
Dim RandNumb As New Random
Dim StrBld As New StringBuilder
If length > 0 Then
Do Until Not History.Contains(StrBld.ToString) AndAlso StrBld.Length >= length
For i As Integer = 0 To length - 1
StrBld.Append(ListChars(RandNumb.Next(0, ListChars.Count - 1)))
Next
Loop
History.Add(StrBld.ToString)
Return StrBld.ToString
Else
Return String.Empty
End If
End Function
End Class