Since I have software that is in need of protection I needed to come up with a simple serial number generator. The format that I decided on was a simple four sets of six alphanumeric sequence. Example: EICDG8-I56803-58BFEI-2D7I9B.
This is a Quick Tip, so there won’t be much details on how the code will be logged and tested for repetitiveness. What I will say is that the serial number will be compared to previous generated numbers within a database of numbers. The database search function is simple and ideally suited for the task.
Since the result will be a four by six alphanumeric I created an array of six integers. I placed a single label on my form, no buttons or other controls or objects. See, I did say this would be simple. Double click on the form to create the On Load sub. Also, add a function to begin the building of the random sequence and the array for the six number sequence. There are no alpha characters in the array, just the digital value of the alpha characters .
Public Class frmMain Dim RandArray(6) As Integer Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load End Sub Private Function BuildSerialNumber() As String End Function End Class
Function BuildSerialNumber() is where the four sixes get put together. Random generation and Alphanumeric sequencing will be done by other functions. I built a random seed generation routine that provides values 0-5 for a SWITCH CASE statement. The SWITCH CASE statement is built within a FOR statement that iterates through the six elements of the array. I will use Randomize() before each Rnd() to insure a good randomization. The SWITCH CASE statement could have been done using two cases, 0 & 1. However, I felt 6 choices, three of each might provide better resolution.
Private Sub getRandom() For i = 0 To 5 ' Each element of the array gets a random number 0 - 9 Randomize() Select Case randomSeed() Case 0 : RandArray(i) = (9 * Rnd()) ' Result will be numeric Case 1 : RandArray(i) = (9 * Rnd()) + 65 ' Result will be alpha A - J Case 2 : RandArray(i) = (9 * Rnd()) Case 3 : RandArray(i) = (9 * Rnd()) + 65 Case 4 : RandArray(i) = (9 * Rnd()) Case 5 : RandArray(i) = (9 * Rnd()) + 65 End Select Next End Sub Private Function randomSeed() As Integer Randomize() Return (5 * Rnd()) End Function
By the way, Select Case (5 * Rnd()) doesn’t work, sorry.
The final function determines whether the element of the array is to be presented as an alpha value or numeric value and returns the six elements in a string. As the numeric elements are assigned 0 – 9, and the alpha elements are assigned A – J, or 65 – 74 we can simply apply an IF statement to do our work. The alpha elements will be converted to their Character form using Chr(int).
Private Function ToAlphaNumeric(ByVal int() As Integer) As String Dim response As String = "" Try For i = 0 To 5 If int(i) < 10 Then ' Is numeric, 0 - 9 response += int(i).ToString Else ' Is alpha, all other valuer are greater than 9 response += Chr(int(i)) End If Next Catch ex As Exception End Try Return response End Function
As you can see the numeric elements are simply added to the string and the alpha elements are converted to their corresponding Characters. Now that the routines are done the serial number can be built. Here we will randomize the array four times using getRandom(), each time adding the alphanumeric values to a string variable using ToAlphaNumeric(BaseCode). At the first three steps the string variable, named “result” will have a dash “-” added to the end of the string before the next step.
Private Function BuildSerialNumber() As String Dim result As String getRandom() result = ToAlphaNumeric(BaseCode) & "-" getRandom() result += ToAlphaNumeric(BaseCode) & "-" getRandom() result += ToAlphaNumeric(BaseCode) & "-" getRandom() result += ToAlphaNumeric(BaseCode) Return result End Function
And finally, to display the result add the following code to the On Load sub:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Label1.Text = BuildSerialNumber() End Sub
I hope this was useful to someone out there. Happy coding.
Disclaimer: The code in this tutorial is not intended as a final product or by no means the only way this project can be coded. The presented code is for educational purposes only and no warranty is granted or implied. I/we are not responsible for any damages as a result of using this code. Use at your own risk.