C:/Musimathics_local/Musimat/MusimatChapter9/C091204b.cpp File Reference

#include "MusimatChapter9.h"

Go to the source code of this file.

Functions

 MusimatChapter9Section (C091204b)
IntegerList swap (IntegerList L, Integer from, Integer to)
Static Void para1 ()
IntegerList shuffle (IntegerList L)
Static Void para2 ()


Function Documentation

MusimatChapter9Section ( C091204b   ) 

Definition at line 2 of file C091204b.cpp.

References para1(), and para2().

00002                                  {
00003 Print("*** Shuffle ***");
00004 /*****************************************************************************
00005 
00006 Shuffle
00007 
00008 We can create a random permutation of a row rather as one would shuffle a deck of cards. 
00009 If we distinguish between the cards and their position in the deck, shuffling consists of swapping 
00010 the positions of all cards a pair at a time. First, we need a way to swap the position of two cards 
00011 in the deck. We can swap the position of two elements in IntegerList like this:
00012 *****************************************************************************/
00013         para1(); // Step into this function to continue.
00014         para2(); // Step into this function to continue.
00015 }

Static Void para1 (  ) 

Definition at line 24 of file C091204b.cpp.

00024                     {
00025 /*****************************************************************************
00026 To shuffle an entire deck of cards (or row of pitch classes), we visit each position in the list from 
00027 first to last in order and swap the card at each position with a card at a randomly chosen other 
00028 position. Because we use Random() to choose the position of the other card to swap, the “other” 
00029 position can be any position in the deck, including the currently selected position; thus we may 
00030 occasionally swap a card with its own position, leaving it where it was. However, in a subsequent 
00031 step, that card might be chosen to be swapped elsewhere.
00032 *****************************************************************************/
00033 }

Static Void para2 (  ) 

Definition at line 44 of file C091204b.cpp.

References shuffle(), and x.

00044                     {
00045 /*****************************************************************************
00046 The first step is to generate a new row with randomRow(), which is stored in IntegerList M. 
00047 Successive values of i and successive elements of M give the indexes of the elements in L that are 
00048 to be swapped. Suppose we have
00049 
00050 L =   {0, 6, 2, 9, 7, 5, 4, 10, 8, 3, 1, 11}; // source row
00051 
00052 M = {5, 1, 0, 4, 6, 7, 9, 3, 10, 8, 11, 2};    // row created in shuffle
00053 
00054 Then each row in table 9.6 shows the intermediate values of L as its elements are being swapped. The 
00055 pattern starts out like this: swap the value in position 0 and the value in position 5; swap the value 
00056 in position 1 with itself; swap the value in position 2 and the value in position 0; swap the value in 
00057 position 3 and the value in position 4; and so on. The result is that every element of the input row is 
00058 swapped randomly with another element, but there’s a chance it might be swapped with itself.
00059 *****************************************************************************/
00060 
00061         Print("*** Shuffling a row ***");
00062         IntegerList x = IntegerList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
00063         Print("A row:", x);
00064         Print("Same row after shuffling:", shuffle(x));
00065 
00066 }}

IntegerList shuffle ( IntegerList  L  ) 

Definition at line 35 of file C091204b.cpp.

References randomRow(), and swap().

00035                                    {
00036         IntegerList M = randomRow(Length(L));                                                           // elements to swap
00037         For (Integer i = 0; i < Length(L); i = i + 1) {
00038                 Integer j = M[i];
00039                 L = swap(L, i, j);
00040         }
00041         Return(L);
00042 }

IntegerList swap ( IntegerList  L,
Integer  from,
Integer  to 
)

Definition at line 17 of file C091204b.cpp.

References x.

00017                                                           {
00018         Integer x = L[to];                              // save target value
00019         L[to] = L[from];                                // swap from Ć to
00020         L[from] = x;                                    // swap to Ć from
00021         Return(L);
00022 }


Generated on Fri Sep 8 23:11:09 2006 for Musimat Chapter 9 Code Examples by  doxygen 1.4.7