[RESOLVED]Help with translation from .NET + explanation
Can anyone please translate this to Classic VB(5)
VB Code:
Public Sub slumpa()
j = Int(35 * Rnd() + 1)
For i = 0 To 6
Do While Array.IndexOf(lotto, j) > -1
j = Int(35 * Rnd() + 1)
Loop
lotto(i) = j
Next i
And can somebody perhaps explain what the 'Do While Array.IndexOf(lotto, j) > -1' part does (I know that the purpose of the code is to give unique values to the variables in the array, but I don't know what it 'does' more precisely). Thanks a lot
Re: Help with translation from .NET + explanation
I didn't test it so give it the try but in general code you posted generates random number, then it checks if current value already exists in the array and if not it will assign to current array item.
"Translated" version does pretty much the same thing:
VB Code:
'VB.Net - Array.IndexOf Method:
'Searches for the specified object and returns the index
'of the first occurrence within the entire one-dimensional
Dim i%, j%, k%
Dim blnExist As Boolean
For i = 0 To 6
blnExist = False
Do While blnExist = False
j = Int(35 * Rnd() + 1)
For k = 0 To UBound(lotto)
If lotto(k) = j Then
blnExist = True
Exit For
End If
Next k
Loop
lotto(i) = j
Next i
End Sub
Re: Help with translation from .NET + explanation
It seems to be looping infinitely when I try it in .NET. Aslo, what does the >-1 mean?
Re: Help with translation from .NET + explanation
Bump. I really need help with this.
Re: Help with translation from .NET + explanation
Quote:
Originally Posted by Ruckus_Gary
...Aslo, what does the >-1 mean?
It means that there is an item in the array that's equal J so it will loop until no match is found.
Re: Help with translation from .NET + explanation
I see. But what I can't figure out is why your solution loops infinitely. Everything seems logical.
Re: Help with translation from .NET + explanation
Random doesn't mean totally unique so there is agreat chance that item already exist in the array.
What you can do is clear array before re-populating it:
VB Code:
[B]Erase lotto[/B]
For i = 0 To 6
'...
Next i
Re: Help with translation from .NET + explanation
*EDIT* I solved it
VB Code:
For i = 0 To 6
exists = False
Do While exists = False
j = Int(35 * Rnd() + 1)
exists = True
For k = 0 To 6
If array(k) = j Then
exists = False
Exit For
End If
Next k
Loop
array(i) = j
Next i
It's currently a little un organized withe the exists variable but I'll fix it.
Thanks a lot for the help
Re: [RESOLVED]Help with translation from .NET + explanation
NO problem. :wave: :thumb: