1. Computing

Distributions in Dealing Cards

Chris's Program Design

By

Rather than throw everything into a single event subroutine as I did, Chris took the time to design a system that relied on real object oriented principals. It's worth studying just for that. The result isn't as easy to read and understand, but it has other benefits that make it clearly superior. Here's a class diagram of the modules.

Chris put the integer array creation code into different classes so they can be used as objects in his main module. These objects are managed as elements of an array themselves:


Dim rnd As New Random
Dim shufflers(0 To 3) As IShuffler
shufflers(0) = New CheckForDuplicatesShuffler(rnd)
shufflers(1) = New ArrayShufflingShuffler(rnd)
shufflers(2) = New LambdaShuffler(rnd)
shufflers(3) = New FisherYatesShuffler(rnd)

Chris also uses an Interface - IShuffler. This isn't necessary and the program runs the same way without it, but it's a nice touch to guarantee that each shuffler class follows the same rules.

Here's a class diagram of Chris's program that will give you an overview of the whole thing:

--------
Click Here to display the illustration
--------

Since "random" is one of the themes here, Chris's implementation of the Random object is worth looking at too. At the Microsoft site, the documentation for the Random object states, "To improve performance, create one Random object to generate many random numbers over time, instead of repeatedly creating a new Random objects to generate one random number." That's exactly what Chris does.

Designing the program to use classes this way makes it possible to simply pass them to another class that gets the timing of the each object:


' Time each shuffle method
Dim theShufflerTimer As New ShufflerTimer(iterations)
For Each shuffler In shufflers
    Dim elapsed As TimeSpan
    elapsed = theShufflerTimer.TimeShuffle(shuffler)
...

and the distribution


' Check the distribution of each shuffle method
Dim theSpread As New SpreadSampler(iterations)
For Each shuffler In shufflers
    spreadResults = theSpread.CheckSpread(shuffler)
...

Chris wasn't satisfied with this, however, and contributed a much more extensive program that contrasts C# with VB in solving this problem. You can read about it in the fourth article in this series here.

You can download Chris's complete initial program here.

  1. About.com
  2. Computing
  3. Visual Basic
  4. Learn VB.NET
  5. Frequency Distributions in Dealing Cards in Games

©2014 About.com. All rights reserved.