by wizbay
10. September 2010 02:53
Simple function to Generate a random alpha-numeric code with any length in VB.Net
'Generate a random alpha-numeric code with any length.
Shared Function GetRandomCode(ByVal length As Int16) As String
Dim num_characters As Integer
Dim i As Integer
Dim txt As String = String.Empty
Dim ch As Integer
num_characters = length
Randomize()
For i = 1 To num_characters
ch = Int((26 + 26 + 10) * Rnd())
If ch < 26 Then
txt = txt & Chr(ch + Asc("A"))
ElseIf ch < 2 * 26 Then
ch = ch - 26
txt = txt & Chr(ch + Asc("a"))
Else
ch = ch - 26 - 26
txt = txt & Chr(ch + Asc("0"))
End If
Next i
Return txt
End Function