How to Generate a Random Password or Verification String
- In some cases we need to generate the random string using the some of the input from the user. This post simply explains about the generating the Random Verification string.
- To generate the random verification string we need to give the input for how many characters we need a output, input Letter (e.g., User Name) and the number for mixing to the string.
- Use the following Random generator code class library we can create the verification codes.
''' <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>
Public 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
Randomize()
Random1 = Rnd()
arrIndex = -1
' IF THE VALUE IS AN EVEN NUMBER WE GENERATE A LETTER, OTHERWISE WE
' GENERATE A NUMBER
' THE NUMBER '111' WAS RANDOMLY CHOSEN. ANY NUMBER WILL DO, WE JUST NEED
' TO BRING THE VALUE ABOVE '0'
If (CType(Random1 * 111, Integer)) Mod 2 = 0 Then
' GENERATE A RANDOM LOCATION IN THE LETTERS CHARACTER ARRAY
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
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
Do While arrIndex < 0
arrIndex = Convert.ToInt16(NumbersArray.GetUpperBound(0) * Random1)
Loop
sb.Append(NumbersArray(arrIndex))
End If
Next
Return sb.ToString
End Function
- To add the Reference of the dll to another project (Window or Web) and create teh object for the class RandomKeyGenerator and call the method Generate().
E.g.
protected void Button1_Click(object sender, EventArgs e)
{
RandomKeyGenerator Rnd = new RandomKeyGenerator();
Rnd.KeyChars = 5;
Rnd.KeyLetters = "Siva";
Rnd.KeyNumbers = "12345";
Response.Write(Rnd.Generate());
}
To use the namespace using RandomKey;
-> To download Random Verification String Generator Click Here



Read User's Comments 