Shuffle Demo

[Home]   [Puzzles & Projects]    [Delphi Techniques]   [Math Topics]   [Library]   [Utilities]

Search

 

Search DelphiForFun.org only

Support DFF

 If you shop at Amazon anyway,  consider using this link. We receive a few cents from each purchase.   Thanks.

In Association with Amazon.com

 

Support DFF

 If you benefit from the website,  in terms of knowledge, entertainment value, or something otherwise useful, consider making a donation via PayPal  to help defray the costs.  (No PayPal account necessary to donate via credit card.)  Transaction is secure.

 

 

Contact

Feedback:  Send an e-mail with your comments about this program (or anything else).

 

Search DelphiForFun.org only

 

 

 

 

Problem Description

This is a Beginners level program using an array of numbers representing cards to "deal" random "hands" of a given size.

Background & Techniques

It takes  less than 50 lines of user written code to shuffle an arbitrary "deck" of numbers and
"deal" them a number of "hands" with a number of "cards" per "hand" as specified by the user.

It was prompted by a user who was trying to do this by randomly choosing card  from the deck without choosing duplicates. That is the hard and slow way to do it. The easy way is to shuffle
the deck, randomly arrange the cards and then choosing "handsize" number at a time from top to bottom to represent the hands.

The key trick is the 10 line "Shuffle" procedure which works from the end of an array of numbers swapping each with a randomly chosen number whose position is less than or equal to the position of the number being swapped.

Running/Exploring the Program 

bulletDownload  executable
bulletDownload source 

Suggestions for Further Explorations

 For standard 52 card arrays it would only take a few more lines of code to display card values as [A,2,3,4...10,J,Q,K] suffixed by as [H,D,C,S].  E.g. 2S for 2 of Spades,  KD for King of Diamonds, etc. 
   

 

Original:  August 05, 2012

Modified:  August 05, 2012

 

  [Feedback]   [Newsletters (subscribe/view)] [About me]
Copyright © 2000-2013, Gary Darby    All rights reserved.