This article deals with the concept of shuffling cards for a computer version of a normal 52 card deck card game. I developed my own algorithm for shuffling and dealing cards to use in my card games. I am sure it is not the most elegant nor is it the best. Therefore, I thought it would be good to check against some of the ideas presented in different forums and articles available. I did find several good ways to accomplish this and some better than mine. This was not a surprise for me. However, I was dumbfounded by the sheer number of times that people asked this question and the number of methods shown in reply that were either not random or had errors. I felt compelled to write this article in order to show one manner in which to accomplish the task and as a warning to verify any method you find on the web before implementing.
To start, you must find a way to generate random numbers. There are several manners to do this. You can even create your own method. If you are like me, you have better things to do. I just used the Random class available in VB .NET. You can access this class by defining a variable as New Random(). You can then access random numbers by using the .Next function of the class (ie Dim RandomNumbers As New Random() then RandomNumbers.Next(0,52)). You will notice the arguments of the Next function. I have chosen these to provide integers from 0 to 51. Your lower bound is the first number and is inclusive of that number. If your lower bound is zero, it can be omitted. Your upper bound is one less than the second number as it is not inclusive. Since I am using an array of integers to represent my deck and array indices start at zero, I want numbers from 0 to 51.
Now that we have the random number generator figured out, it is time to figure out how to implement it into our shuffling process. I have included the code I used below. I include this algorithm in a function. You will see reference to an array cards(index). I have defined this variable outside of the function in order to use it throughout the program. It was defined as: Dim cards(51) as Integer.
Dim shuffleCount As Integer
Dim currentCard As Integer
Dim RandomNumbers As New Random()
Dim MyRandomNumber As Integer
For counter = 0 To 51
cards(counter) = counter
For shuffleCount = 1 To 10000
MyRandomNumber = RandomNumbers.Next(52)
currentCard = cards(0)
For counter = 1 To MyRandomNumber
cards(counter – 1) = cards(counter)
cards(MyRandomNumber) = currentCard
This code is actually not very sophisticated. I first load the cards into the deck represented by the card number in the cards.dll library. I chose to keep the card number as is in the cards array and then separate it into face and suit as needed. Next, I get to the task of shuffling. In essence, I simply take the bottom card of the deck and place it at a random point in the deck. I do this a total of ten thousand times. The number of times is arbitrary. However, it should be enough times to shuffle the deck thoroughly and not so many that the user is waiting on you.
That is all there is to it. I hope this article helps you to get off to a good start in programming your own VB.Net card game.