2

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
Emre Salum
  • 33
  • 5
  • 1
    Don´t create `Random` objects in your loop but only create one. So move `Dim Rand As New Random()` before the loop. – Alex B. Feb 01 '17 at 09:32
  • @AlexB. oh funny how simple it is. Working now thanks. But could you explain more please? I'd like to know why i didn't have the same problem when i had msgbox in loop. – Emre Salum Feb 01 '17 at 09:41
  • 1
    Basically it is due to .NET calculates random numbers with a [random-number generator](http://en.wikipedia.org/wiki/Pseudorandom_number_generator). This meens the numbers are produced with an determenistic algorithm and thus are not 100% random. One common way to obfuscate these statistical results is to take the current timestamp into consideration. In your example with MsgBox the timestamps are more likely to be different because you need to close the window first, where in your second example the timestamp between creaating the random objects are almost the same. – Alex B. Feb 01 '17 at 09:49
  • 1
    I´m not a .NET framwork developer but John Skeet wrote an [article](http://csharpindepth.com/Articles/Chapter12/Random.aspx) on that theme. Also Marc gave a good [answer](http://stackoverflow.com/a/768001/2882256) – Alex B. Feb 01 '17 at 09:52
  • 1
    [This MS article](https://msdn.microsoft.com/en-us/library/system.random(v=vs.110).aspx) is also very helpful – Alex B. Feb 01 '17 at 09:57

1 Answers1

1

@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.

Emre Salum
  • 33
  • 5