Emre Salum
Emre Salum

Reputation: 33

For loop not working right unless i add Msgbox inside of loop

Hello there everyone.

I have little problem, didn't make sense at all. So i have kinda simple for loop. I want to create random integers and remove index of specific array by that integer.

Working perfect:

   For i = 1 To CInt(rastgelesoru.Text)
            Dim Rand As New Random()
            Dim xIndex As Integer = Rand.Next(0, AList.Count - 1)
            Dim SelectedValue = AList(xIndex)
            Dim eklepanelrnd As Panel = CType(containerpanel.Controls(SelectedValue), Panel)
            If eklepanelrnd.Tag = "1" Then
                MsgBox(xIndex)
                containerpanelrastgele.Controls.Add(eklepanelrnd)
            End If
            AList.RemoveAt(xIndex)
    Next

For example i have 500 element in array. When i add message box like above, it works perfect. I get random numbers. (100,65,355,27,472 last output for 5). But when i remove msgbox line i get Consecutive numbers everytime. First i thought it might be really 'random' but no. Everytime i get Consecutives. (23,24,25,160,161 last output for 5 without msgbox line.)

Not working properly without msgbox line.

   For i = 1 To CInt(rastgelesoru.Text)
            Dim Rand As New Random()
            Dim xIndex As Integer = Rand.Next(0, AList.Count - 1)
            Dim SelectedValue = AList(xIndex)
            Dim eklepanelrnd As Panel = CType(containerpanel.Controls(SelectedValue), Panel)
            If eklepanelrnd.Tag = "1" Then

                containerpanelrastgele.Controls.Add(eklepanelrnd)
            End If
            AList.RemoveAt(xIndex)
    Next

Upvotes: 2

Views: 64

Answers (1)

Emre Salum
Emre Salum

Reputation: 33

@AlexB. on comments.

Don´t create Random objects in your loop but only create one. So move Dim Rand As New Random() before the loop.

Working perfect now. Thanks <3 Have a wonderful day.

Upvotes: 1

Related Questions