Trying to Produce the numbers (1-8) randomly and mutually exclusive (getting an endless loop though)
This program produces 7 random Numbers..but when you try to set Redim Number to (7) to receive an eighth index output, it creates an endless loop because
arrayused.length gets set to 9 and the Index(8) value is never used and thats a problem which is causing the loop to never exit.
The original program was presented on this website in another lanquage and I have converted it to english. I spent about 5 hours debugging and I just dont have enough brains to figure out how to fix the endless looping that occurs if array is Redimmed to (7)
This randomization needed is intended to help me create an 8-man tournament bracket for Bowling Tournaments.
The original post was submitted by
m2s87 in another language (possibly french?)
Someone please help me .. reading tons of books on vb.net lately as my 6 years at tech school has not been enough knowledge yet..
Please feel free to send suggestions.... Thank You
Schmit35@msn.com---IT Graduate 2004 Oregon Institute of Technology
CODE
Public Class Form1
'Create empty string of 8 numbers
Dim number(6) As String 'This needs to be (7) but causes an endless loop when changed
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
ReadingRandomMutuallyExclusiveArray(number, 8) 'Number of members
End Sub
Sub ReadingRandomMutuallyExclusiveArray(ByRef arraytorandomize, ByVal MaxNumber)
Dim a As String
Dim arrayused() As String
Dim i As Integer
Dim ArrayIndexUsed As Integer
Randomize()
' ReDim Preserve arrayused(7)
arrayused = Split(GeneratedNumberString(MaxNumber), ",")
'-----------------------------------------------------------------------------------------------------
For i = LBound(arraytorandomize) To UBound(arraytorandomize)
Do
ArrayIndexUsed = Int(Rnd() * (MaxNumber - 1) + 1) 'ArrayIndexUsed = Random Number Picked
'DoEvent
Loop While arrayused(ArrayIndexUsed) < 1
arrayused(ArrayIndexUsed) = 0 'This sets picked out array value as zero
arraytorandomize(i) = ArrayIndexUsed 'Massiv is second array
a = Join(number, ",") 'number is picked out of arrayused and create new list of 8
TextBox1.Text = a & vbCrLf
Next i
Exit Sub
End Sub
Function GeneratedNumberString(ByVal RemainingNumberstoRandomize) 'Creates 8,7,6,5,4,3,2,1
If RemainingNumberstoRandomize > 0 Then GeneratedNumberString = RemainingNumberstoRandomize _
& "," & GeneratedNumberString(RemainingNumberstoRandomize - 1)
If RemainingNumberstoRandomize = 1 Then RemainingNumberstoRandomize = 0
End Function
Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
ReadingRandomMutuallyExclusiveArray(number, 8)
End Sub
End Class