Introduction
I had to create a piece of code that will give me a set of randomly generated keys. Each key had to consist of letters and numbers, and the letters could be either (randomly) lowercase or uppercase.
Looking at some sample code on the web, I could not find what I needed. All the examples I found either generated numbers only, or letters only. I also wanted this random key to be based on a certain rule that made it "not straightforward" random, so, I came up with this little piece of code.
Using the code
The way it works is as follows:
You feed it with a string of letters: KeyLetters, a string of numbers: KeyNumbers, and how many characters you want the random key to be: Keychars. We then call Generate() which goes and generates a random number through the Randomize() statement and the Rnd() function.
Multiply Rnd() by 111 -could be any number we choose which is sufficient enough to bring the value above zero. If the resulting number is an even number, then our random character will be a letter. If we get an odd number, then our random character will be a number. To generate a random character, we generate a random index for one of the character arrays (depending on what our random character will be). Once we have an index which is >= o, we use it to get the value in the corresponding index in the character array.
If we are generating a number, then that is our random character. If we are generating a letter, then we need to determine whether we want it uppercase or lowercase. For that purpose we generate another random number by multiplying the value of Rnd() by 99 – could be any other number too – and then determine whether the result is even or odd. This time, we capitalize the letter if we get an odd number, otherwise we leave it as it is.
And so on, … the loop keeps "looping" while using, and we use a StringBuilder to construct our resulting string , until we've generated the desired number of characters for our Random Key. We convert the StringBuilder to String and return it with the function.
Note: XML comments in the source code were generated using "VBXC - VB.NET XML Commentor beta 3". I highly recommend it.
Module Module1 SubMain() Dim KeyGen As RandomKeyGenerator Dim NumKeys AsInteger Dim i_Keys AsInteger Dim RandomKey AsString ''' MODIFY THIS TO GET MORE KEYS - LAITH - 27/07/2005 22:48:30 - NumKeys = 20 KeyGen = New RandomKeyGenerator KeyGen.KeyLetters = "abcdefghijklmnopqrstuvwxyz" KeyGen.KeyNumbers = "0123456789" KeyGen.KeyChars = 12 For i_Keys = 1 To NumKeys RandomKey = KeyGen.Generate() Console.WriteLine(RandomKey) Next Console.WriteLine("Press any key to exit...") Console.Read() End Sub End Module RandomKeyGenerator.vb
OptionStrictOn 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 AsString Dim Key_Numbers AsString Dim Key_Chars AsInteger Dim LettersArray AsChar() Dim NumbersArray AsChar() ''' <date>27072005</date><time>071924</time> ''' <type>property</type> ''' <summary> ''' WRITE ONLY PROPERTY. HAS TO BE SET BEFORE CALLING GENERATE() ''' </summary> ProtectedFriendWriteOnlyProperty KeyLetters() AsString Set(ByVal Value AsString) Key_Letters = Value EndSet EndProperty ''' <date>27072005</date><time>071924</time> ''' <type>property</type> ''' <summary> ''' WRITE ONLY PROPERTY. HAS TO BE SET BEFORE CALLING GENERATE() ''' </summary> ProtectedFriendWriteOnlyProperty KeyNumbers() AsString Set(ByVal Value AsString) Key_Numbers = Value EndSet EndProperty ''' <date>27072005</date><time>071924</time> ''' <type>property</type> ''' <summary> ''' WRITE ONLY PROPERTY. HAS TO BE SET BEFORE CALLING GENERATE() ''' </summary> ProtectedFriendWriteOnlyProperty KeyChars() AsInteger Set(ByVal Value AsInteger) Key_Chars = Value EndSet EndProperty ''' <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() AsString Dim i_key AsInteger Dim Random1 AsSingle Dim arrIndex As Int16 Dim sb AsNew StringBuilder Dim RandomLetter AsString ''' 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 INDEX IN THE LETTERS ''' CHARACTER ARRAY - LAITH - 27/07/2005 18:47:44 - DoWhile 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 EndIf sb.Append(RandomLetter) Else ''' GENERATE A RANDOM INDEX IN THE NUMBERS ''' CHARACTER ARRAY - LAITH - 27/07/2005 18:47:44 - DoWhile arrIndex < 0 arrIndex = _ Convert.ToInt16(NumbersArray.GetUpperBound(0) _ * Random1) Loop sb.Append(NumbersArray(arrIndex)) EndIf Next Return sb.ToString EndFunction End Class |
Source Code:Download demo project - 2.36 KbDownload source - 7.64 Kb
Add a Comment
<<Home