Shuffling Cards in VB .NET: A Tutorial on Random Assignment of Cards

This article is about the concept of shuffling cards for a computer version of a normal 52-card card game. I developed my own algorithm for shuffling and dealing cards to use in my card games. Surely it is not the most elegant nor is it the best. Therefore, I thought it would be good to collate some of the ideas presented in different forums and articles available. I found several good ways to accomplish this and some better than mine. This was not a surprise to me. However, I was dumbfounded by the sheer number of times people have asked this question and the number of methods that were shown in response that were not random or had bugs. I felt compelled to write this article to show a way to accomplish the task and as a warning to check any method you find on the web before implementing it.

To get started, you need to find a way to generate random numbers. There are several ways to do this. You can even create your own method. If you’re 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 like New Random(). You can then access random numbers using the class’s .Next function (ie Dim RandomNumbers As New Random() and then RandomNumbers.Next(0,52)). You’ll notice the arguments to the Next function. I have chosen them to provide integers from 0 to 51. Their lower bound is the first number and includes that number. If its lower bound is zero, it can be omitted. Its upper limit is one less than the second number, since it is not inclusive. Since I’m using an array of integers to represent my deck and the indices of the array start at zero, I want numbers from 0 to 51.

Now that we’ve figured out the random number generator, it’s time to figure out how to implement it in our shuffling process. I have included the code I used below. I include this algorithm in a function. You will see a reference to a card array (index). I have defined this variable outside the function to use it throughout the program. It was defined as: Dim cards(51) as Integer.

Sample code:

Dim shuffle Count as integer

Dim current card as integer

Dim RandomNumbers as new Random()

Dim MyRandomNumber as integer

For counter = 0 to 51

cards(counter) = counter

next

For shuffleCount = 1TB 10000

MyRandomNumber = RandomNumbers.Next(52)

currentcard = cards(0)

For counter = 1 TB MyRandomNumber

cards(counter – 1) = cards(counter)

next

cards(MyRandomNumber) = current card

next

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 it is in the card matrix and then separate it into heads and suits as needed. Next, I get to the task of shuffling. In essence, I just take the bottom card of the deck and place it on a random spot 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 well and not so many that the user is waiting for you.

That’s all you have to do. I hope this article helps you get off to a good start in programming your own VB.Net card game.

Website design By BotEap.com

Add a Comment

Your email address will not be published. Required fields are marked *