Visual Basic for Applications/The Knuth String Shuffle

Summary

 * This code module includes both a Fisher-Yates character shuffling routine for strings, and a Durstenfeld-Knuth routine to shuffle one-dimensional arrays.
 * The procedure FisherYatesStrShuffle shuffles characters of a single string. This is limited to shifting single characters around within one string.
 * Procedure KnuthArrShuffle sorts the elements of a one-dimensional array. The procedure is limited only by what can be stored in array elements.
 * The two methods are pseudo-random and bias-free.  Elsewhere, the use of a random generator does not necessarily guarantee that the results will be free from bias.
 * The code can work in any of the MS Office applications that support VBA.

Code Notes

 * The Fisher-Yates shuffle applies a pseudo random selection method.  It is described here for characters in a string but a related method, the  Durstenfeld-Knuth method is preferred for arrays.
 * Taking each element of a string in sequence for repositioning leaves one end of the result string badly biased. The Knuth algorithm instead proposes a random position within the string. The element at that position is then accumulated into the output and removed from the original.   Subsequent selections are made in the same way from the ever shortened string.
 * Note that there is still the possibility of a given character being unmoved in the process, but only within expectation.
 * Set the number of strings required with variable Cycles in the top procedure. The Immediate Window has proved the best place for display and copying.
 * It should be pointed out that any attempt to avoid the unmoved elements, will not only change the random nature of the shuffle but prevent the use of other than non-repeat strings.  That is to say strings with repeated characters could not then be shuffled.
 * The Durstenfeld-Knuth method for arrays differs only slightly from that of the Fisher-Yates implementation.
 * To reduce processing, and no doubt to overcome the burden of removing an element from the middle of an array during shortening, the algorithm instead overwrites the element selected for output with the last element.  In this VBA implementation the array is then conveniently shortened by one element with Redim Preserve.
 * See Fisher Yates Shuffle for a good description of both methods.

The VBA Code Module
Copy all of the code below into say, an MS Excel standard module, save the workbook as an xlsm file type, and run either of the test procedures to test the requisite code. Be sure to open the Immediate Window for output.