Quote:
Originally Posted by rcadble Why not make it in complete function form? You should try make it a function where your user can just input the settings (as it is now, you'd have to make a new function every time you wanted to change the length). Code: Public Function GenerateCodeRcad(ByVal lngLength As Integer) As String
Dim i As Integer
Dim strRand As String
Dim intRand As Integer
Randomize
For i = 0 To lngLength Step 1
intRand = Int(Math.Rnd() * 3) + 1
If intRand = 1 Then
strRand = strRand & Int(Math.Rnd() * 10)
ElseIf intRand = 2 Then
strRand = strRand & Chr(Int(Math.Rnd() * (26) + 65))
ElseIf intRand = 3 Then
strRand = strRand & Chr(Int(Math.Rnd() * (26) + 97))
End If
Next
GenerateCodeRcad = strRand
End Function Unfortunately, the mid function is pretty slow for vb6, so using chr() with random ascii values would speed it up. (Mine averaged 0.93 microseconds faster, by testing them both 100,000 times). |
rcadble:
your code generates one extra character
Code:
Public Function GenerateCodeRcad(ByVal lngLength As Integer) As String
Dim i As Integer
Dim strRand As String
Dim intRand As Integer
Randomize
lnglenth = lnglenth - 1
For i = 0 To lngLength Step 1
intRand = Int(Math.Rnd() * 3) + 1
If intRand = 1 Then
strRand = strRand & Int(Math.Rnd() * 10)
ElseIf intRand = 2 Then
strRand = strRand & Chr(Int(Math.Rnd() * (26) + 65))
ElseIf intRand = 3 Then
strRand = strRand & Chr(Int(Math.Rnd() * (26) + 97))
End If
Next
GenerateCodeRcad = strRand
End Function