Random Assortment

How do you randomly assort a string of values? This is to virtually "shuffle" cards.
[99 byte] By [Modderman] at [2007-12-16]
# 1

You can use the Fisher-Yates shuffle to produce a good random distribution of objects:

' Some things to shuffle

Dim myStrings() As String = {"One", "Two", "Three", "Four", "Five"}

' Our random number source

Dim rand As New Random

For i As Integer = 0 To myStrings.Length - 1

' Pick an element to swap around

Dim swapIndex As Integer = rand.Next(i, myStrings.Length)

' Switch them

Dim temp As String = myStrings(i)

myStrings(i) = myStrings(swapIndex)

myStrings(swapIndex) = temp

Next

' Print the results

For Each str As String In myStrings

Console.WriteLine(str)

Next

RyanCavanaughMS at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...