TUTORIAL FOR "SHUFFLING" AN ARRAY OF OBJECTS

This tutorial is designed to run in the VB.NET environment since it is probably one of the easier languages to read.  It is easily adaptable to just about any other language, by adding semicolons and/or the format of the procedures and commands.  I will randomize an array of strings representing a standard deck of cards since it is an example everyone can relate to, however it will work for any array of objects.  I have used the procedure to randomize pictures in an album and buttons for various games.  An "Object oriented" approach has been used in order to provide portability of the code.  2 procedures have been written:  the shuffle( ) procedure and the swap( ) procedure.  there is also an initialization section in which the array is established prior to running the procedures and a call to the procedure. 
the first section is the initialization of the array.  each card has its own 2 letter designation (AH = Ace of Hearts, 2H = 2 of Hearts . . .)

 

Private strDeck() As String = {"AH", "2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "10H", "JH", "QH", "KD", "AD", "2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "10D", "JD", "QD", "KD", "AC", "2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "10C", "JC", "QC", "KC", "AS", "2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "10S", "JS", "QS", "KS"}
The call to the procedure is usually linked to an "event" (like a button push).  It is a single line which passes two variables to the procedure:  The array to be randomized is the first variable, and an integer representing the number of times the array will be "shuffled".  Similar to the amount of times a deck of cards is shuffled.  In this example the deck will be shuffled 7 times which is considered a thoroughly shuffled.
  shuffle(strDeck, 7)

Now for the main shuffle procedure

  The array is passed ByRef so that the array elements can change position.  The integer will not need to change therefore can be passed ByVal.  For more information on these terms you will have to look in the help section of Visual Studio.NET for now.  I have no tutorial for these terms.

The shuffle procedure involves swapping each element of the array with a different, randomly chosen, element.  The first For...Next loop specifies the number of times the array will be shuffled.  An embedded For...Next loop counts through the array and swaps a random position in the array with it. 

the rndPosition is an instance of the Random function which is built in to the .NET framework.  Again for a more in depth study of this function, go to the help section of VB.NET for now.   All I will say is that after you construct the rndPosition variable, you can use it to generate any random number you want simply by calling the .Next(MinNumber, MaxNumber) method of the random Function.  For example rndPosition.Next(0,51) will return an integer from 0 to 51.

Anyway, that's about all there is to it.  Relatively easy, highly optimized, and ultra portable.  If you are still having trouble understanding it, copy and paste the below code to a text editor, or the VB IDE and manually shorten the variable names.  I find code easier to understand with shorter variables, but I use longer ones for documentation purposes.  

ephimany

 

Private Sub shuffle(ByRef arrayToBeShuffled As Array, ByVal numberOfTimesToShuffle As Integer)

     Dim rndPosition As New Random(DateTime.Now.Millisecond)

     For i As Integer = 1 To numberOfTimesToShuffle

          For i2 As Integer = 1 To arrayToBeShuffled.Length

               swap(arrayToBeShuffled(rndPosition.Next(0, arrayToBeShuffled.Length)), arrayToBeShuffled(rndPosition.Next(0, arrayToBeShuffled.Length)))

          Next i2

     Next i

End Sub

For those programming languages that do not have a built in Swap function (just about all of the relatively newer ones), the below is a simple function which swaps anything.
 

Private Sub swap(ByRef arg1 As Object, ByRef arg2 As Object)

     Dim strTemp As String

     strTemp = arg1

     arg1 = arg2

     arg2 = strTemp

End Sub

If you are still confused, don't worry, just contact me and I will try to explain it differently.  I tend to ramble at times.  Anyway, I enjoy feedback of any type.
E-Mail:

Steve Danhof

 

Free Hit Counter