You create a new random sequence every time you call RandomPosition
but, 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 rand
variable once, then just continue to use it, something like:
Dim rand as New Random()
Dim top = rand.Next (1, 100)
Dim left = rand.Next (1, 100)
Alternatively, if you really want it in its own function, make the random generator static so that it maintains its state across calls:
Function RandomPosition()
Static rand = New Random()
Return rand.Next(1, 100)
End Function
The following complete VB2010 program shows this in action:
Module Module1
Function RandomPosition()
Static rand As Random = New Random()
Return rand.Next(1, 100)
End Function
Sub Main()
Dim top = RandomPosition()
Dim left = RandomPosition()
MsgBox("top = " & CStr(top) & ", left = " & CStr(left))
End Sub
End Module
It outputs, on various runs:
top = 7, left = 93
top = 45, left = 90
top = 44, left = 62