# How to generate a random number sequence that is non-repeating in visual basic .net ? [Answered]RSS

Last post Apr 04, 2011 04:11 AM by frez

Member

10 Points

23 Posts

### How to generate a random number sequence that is non-repeating in visual basic .net ?

I am writing a matching program, and need 24 objects to randomly appear, each twice, across 48 spaces. I figured the best way for me to do this would be to assign the positions a number 1 through 48. but the problem is I can't find a way to generate a random number sequence in visual basic 9 .net

I found many examples in visual basic .net but the conversion doesn't work. If not some code, many some example programs that either use a random number sequence or randomly sets the position of objects.

Thanks.

Contributor

3652 Points

896 Posts

### Re: How to generate a random number sequence that is non-repeating in visual basic .net ?

You want to use a shuffle routine, my language of choice is C#, so I ran my code through a converter to come up with this:

```Protected Sub Button1_Click(sender As Object, e As EventArgs)
Dim shuffleThis As Integer() = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, _
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, _
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, _
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24}
Suffle(shuffleThis)

End Sub

Public Sub Suffle(Of T)(listToShuffle As IList(Of T))
If listToShuffle.Count <= 1 Then
Return
End If

Dim random = New Random()

For indexToMoveFrom As var = listToShuffle.Count - 1 To 0 Step -1
Dim indexToMoveTo = random.[Next](indexToMoveFrom + 1)

' Swap
Dim itemToMove = listToShuffle(indexToMoveFrom)
listToShuffle(indexToMoveFrom) = listToShuffle(indexToMoveTo)
listToShuffle(indexToMoveTo) = itemToMove
Next
End Sub```
MCSD, MCPD, MCTS