# having problem with random arraay number

Hi, all:

currently i've a question that need me to create an integer type of array which this array have some random number in it, and it is NOT REPEATABLE, i've try my own coding(totally mine, no copy), and i think is quite find, but when i try on it, it just repeatly at the ending 2 array element(other won't), why? here my code(i seperate the code into 2 part):

[code]
' at cmdRandNum_Click
Private Sub cmdRandNum_Click()
' notice here i use static array
Dim rndIntArr(0 to 5) as Integer, rndN as Integer, i as Integer

For i = 0 To 5
genRndAgain:
rndN = Format(Rnd * 10, "#") ' the random occur here
If verifyArr (rndN, i) = False then
rndIntArr(i) = rndN
Else
GOTO genRndAgain ' if there is/are repeat in the array
End If
Next i

End Sub

' at the function to get verify array element
Function verifyArr (rndNum as Integer, arrEle as Integer) as Boolean
Dim j as Integer,

For j = 0 To arrEle
If rndIntArr(j) = rndNum Then
verifyArr = True ' if found repeat
Else
found = False ' need to continue to the last element
End If
Next j

If found = False Then verifyArr = False
End Function
[/code]

But, but... i got a copy from my friend where he uses the same logic/algorithm but by using the "dynamic" array and he put everything in a Sub method, it just come to perfect, why? the code are as follow:

[code]
Private Sub Command1_Click
' notice here i use static array
Dim rndIntArr() as Integer, rndN as Integer, i as Integer, j as Integer

ReDim Preserve rndIntArr(0)
For i = 1 To 5
genRndAgain:
rndN = Format(Rnd * 10, "#") ' the random occur here
For j = 0 To i
If rndIntArr(j) <> rndN Then
rndIntArr(j) = rndN
If i <= 5 Then ReDim Preserve rndIntArr(i + 1)
Else
GOTO genRndAgain ' if there is/are repeat in the array
End If
Next j
Next i

End Sub
[/code]

these code may be not exactly the same because i not having the code right now, will be updated as soon as possible, thank you for looking at it

• [b][red]This message was edited by Mike_AB1 at 2005-2-28 7:8:10[/red][/b][hr]
: Hi, all:
:
: currently i've a question that need me to create an integer type of array which this array have some random number in it, and it is NOT REPEATABLE, i've try my own coding(totally mine, no copy), and i think is quite find, but when i try on it, it just repeatly at the ending 2 array element(other won't), why? here my code(i seperate the code into 2 part):
:
: [code]
: ' at cmdRandNum_Click
: Private Sub cmdRandNum_Click()
: ' notice here i use static array
: Dim rndIntArr(0 to 5) as Integer, rndN as Integer, i as Integer
:
: For i = 0 To 5
: genRndAgain:
: rndN = Format(Rnd * 10, "#") ' the random occur here
: If verifyArr (rndN, i) = False then
: rndIntArr(i) = rndN
: Else
: GOTO genRndAgain ' if there is/are repeat in the array
: End If
: Next i
:
: End Sub
:
:
: ' at the function to get verify array element
: Function verifyArr (rndNum as Integer, arrEle as Integer) as Boolean
: Dim j as Integer,
:
: For j = 0 To arrEle
: If rndIntArr(j) = rndNum Then
: verifyArr = True ' if found repeat
: Else
: found = False ' need to continue to the last element
: End If
: Next j
:
: If found = False Then verifyArr = False
: End Function
: [/code]

Your code is much better. Only problem is in the Function verifyArr.
Say
rndIntArr(0)=0
rndIntArr(1)=2
rndIntArr(2)=3
rndNum = 2 '''just happenes to be the same as the first elememt.
[code]
For j = 0 To arrEle
If rndIntArr(j) = rndNum Then
verifyArr = True ' if found repeat
Else
found = False ' need to continue to the last element
End If
Next j
[/code]
During the second pass (J=1) it will see rndIntArr(1) = rndNum and set
verifyArr = True.
BUT during next pass (J=2) it will see rndIntArr(2) <> rndNum ans set
Found=False.
Note: After exiting the loop you set your result depend on found variable while you should live it alone so the fulction must look like:

Function verifyArr (rndNum as Integer, arrEle as Integer) as Boolean
[code]
Dim j as Integer,

For j = 0 To arrEle
If rndIntArr(j) = rndNum Then
verifyArr = True ' if found repeat you set sesult here and do not overwrite it
Next j

End Function
[/code]
In addition 'GOTO' is a bad practice. Try to avoid it. Look how the first loop may look:
[code]
Private Sub cmdRandNum_Click()
' notice here i use static array
Dim rndIntArr(0 to 5) as Integer, rndN as Integer, i as Integer

For i = 0 To 5
rndN = Format(Rnd * 10, "#") ' the random occur here
If verifyArr (rndN, i) = False then
rndIntArr(i) = rndN
Else
i=i-1 ' This way you do not need to use GOTO. 'For i...' statement will inctement i again
End If
Next i

End Sub
or you can do like:
Private Sub cmdRandNum_Click()
' notice here i use static array
Dim rndIntArr(0 to 5) as Integer, rndN as Integer, i as Integer

For i = 0 To 5
do
rndN = Format(Rnd * 10, "#") ' the random occur here
loop until verifyArr(rndN, i) = false '''Keep checking while generated number is not unique. exit this loop right after generating a valid number.
rndIntArr(i) = rndN
Next i

End Sub
[code]
Mike