Movian
04-08-2011, 02:12 PM
Hey ok so i have a little problem i need to generate a string of 32 characters in UTF-8 format(as far as i can tell any string with standard ascii is in UTF-8 with some exceptions)
Now i wrote (and borrowed) the following functions to do this. However as soon as i put the tmpnum = randomnumber line in a loop it generates 32 of the same characters. if its outside the loop it appears to work fine.... any suggestions ?
Option Compare Database
Private Sub Command0_Click()
MsgBox RandString32
End Sub
Public Function RandString32() As String
Dim counter As Integer
Dim wordstring As String
Dim tmpnum As Integer
For counter = 1 To 32
Do While Not IsBetween(tmpnum, 48, 57) And Not IsBetween(tmpnum, 65, 90) And Not IsBetween(tmpnum, 97, 122)
tmpnum = RandomNumber(48, 122)
Loop
wordstring = wordstring & Chr(tmpnum)
Next
RandString32 = wordstring
End Function
Public Function IsBetween(value As Integer, lower As Integer, higher As Integer) As Boolean
If value >= lower Then
If value <= higher Then
IsBetween = True
Exit Function
End If
End If
IsBetween = False
End Function
Public Function RandomNumber(Lowest As Long, Highest As Long) As Integer
' Generates a random whole number within a given range
Randomize
RandomNumber = Int((Highest - Lowest + 1) * Rnd + Lowest)
End Function
Now i wrote (and borrowed) the following functions to do this. However as soon as i put the tmpnum = randomnumber line in a loop it generates 32 of the same characters. if its outside the loop it appears to work fine.... any suggestions ?
Option Compare Database
Private Sub Command0_Click()
MsgBox RandString32
End Sub
Public Function RandString32() As String
Dim counter As Integer
Dim wordstring As String
Dim tmpnum As Integer
For counter = 1 To 32
Do While Not IsBetween(tmpnum, 48, 57) And Not IsBetween(tmpnum, 65, 90) And Not IsBetween(tmpnum, 97, 122)
tmpnum = RandomNumber(48, 122)
Loop
wordstring = wordstring & Chr(tmpnum)
Next
RandString32 = wordstring
End Function
Public Function IsBetween(value As Integer, lower As Integer, higher As Integer) As Boolean
If value >= lower Then
If value <= higher Then
IsBetween = True
Exit Function
End If
End If
IsBetween = False
End Function
Public Function RandomNumber(Lowest As Long, Highest As Long) As Integer
' Generates a random whole number within a given range
Randomize
RandomNumber = Int((Highest - Lowest + 1) * Rnd + Lowest)
End Function