PDA

View Full Version : Solved: Random Number the same



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

hansup
04-08-2011, 07:04 PM
Do this do what you want?

Public Function RandString32() As String
Dim counter As Integer
Dim wordstring As String
Dim tmpnum As Integer
Dim OK As Boolean

For counter = 1 To 32
OK = False
Do While Not OK
tmpnum = RandomNumber(48, 122)
If (tmpnum >= 48 And tmpnum <= 57) _
Or (tmpnum >= 65 And tmpnum <= 90) _
Or (tmpnum >= 97 And tmpnum <= 122) Then
OK = False
Else
OK = True
End If
Loop
wordstring = wordstring & Chr(tmpnum)
Next
RandString32 = wordstring
End Function

Movian
04-10-2011, 04:13 PM
With one minor change this did the trick.

Had to switch the True False logic as i need it to come back with only text. while the example you gave was coming back with only special characters that i wanted to exclude with the IF.


Thanks for the help... so whats the difference that makes your loop work and mine not ?

Public Function RandString32() As String Dim counter As Integer Dim wordstring As String Dim tmpnum As Integer Dim OK As Boolean For counter = 1 To 32 OK = False Do While Not OK tmpnum = RandomNumber(48, 122) If (tmpnum >= 48 And tmpnum <= 57) _ Or (tmpnum >= 65 And tmpnum <= 90) _ Or (tmpnum >= 97 And tmpnum <= 122) Then OK = True Else OK = False End If Loop wordstring = wordstring & Chr(tmpnum) Next RandString32 = wordstring End Function

hansup
04-10-2011, 05:13 PM
Aha! I wondered whether I'd confused which were the keeper values and which not. That "Not IsBetween" jazz made it tougher for me to sort out.

What was the point of that anyway? You took a simple clear expression "tmpnum >= 48 And tmpnum <= 57" and obfuscated it as "IsBetween(tmpnum, 48, 57)". Why?!!!!!

If you're enamored of the word "Between", try
Eval(tmpnum & " Between 48 And 57")

Anyway I think what was happening in your original version is that once you assigned a value to tmpnum (eg 71) which satisfied your Do While loop condition, you didn't have an opportunity to assign a different value to it. You could have fixed that by setting tmpnum to zero immediately before the Do While.

You could have set a watch on tmpnum and stepped through the code in break mode --- then you would have seen that tmpnum didn't change again after it was first assigned a value.

But the way you wrote that makes it hard to figure out. I re-wrote in a way I hoped would be less confusing. However, that's not the way I would do it for myself. For me, I would load the keeper values into a Scripting.Dictionary, then check whether the current value of tmpnum was one I wanted to keep.

If dctKeepers.Exists(tmpnum) = True Then