Dim top = RandomPosition()
Dim left = RandomPosition()
End Sub
Function RandomPosition()
Dim rand As New Random()
Dim number = rand.Next(1, 100)
Return number
End Function
Hi guys I am trying to get 2 different random values (for now. Once this works I will need a few more). The problem is that with the above code top and left always equal the same random number.
You create a new random sequence every time you call
RandomPositionbut, because you’re calling it in quick succession, they’ll have the same seed (based on time). Same seed means same sequence.You should create the
randvariable once, then just continue to use it, something like:Alternatively, if you really want it in its own function, make the random generator static so that it maintains its state across calls:
The following complete VB2010 program shows this in action:
It outputs, on various runs: