00001 #include "MusimatChapter9.h" 00002 MusimatChapter9Section(C091204a) { 00003 Print("*** Sampling without Replacement ***"); 00004 /***************************************************************************** 00005 00006 Sampling without Replacement 00007 00008 We can generate a randomly selected 12-tone row, for example, 00009 by putting 12 balls in an urn, each marked with one of the chromatic pitch classes, and draw them out 00010 one at a time without replacement, thereby guaranteeing that no pitch class is chosen more than once. 00011 00012 Random(0, 11) returns a random integer between 0 and 11 with equal probability. But it could 00013 return the same value multiple times, so we must keep track of which pitch classes have been cho- 00014 sen to ensure that it eventually picks one of each. This function takes one argument, N, determining 00015 the length of the row. 00016 *****************************************************************************/ 00017 para1(); // Step into this function to continue. 00018 } 00019 00020 IntegerList randomRow(Integer N) { 00021 IntegerList L; // keep track of pitches chosen so far 00022 IntegerList M; // used to build up random 12-tone row 00023 Integer i; 00024 00025 // set all list elements to zero, which means "unused" 00026 For (i = 0; i < N; i = i + 1) { 00027 L[i] = 0; 00028 } 00029 00030 // build up M, marking off elements in L when they are chosen 00031 i = 0; 00032 While (i < N) { 00033 Integer x = Random(0, N - 1); // returns integer random value 00034 If (L[x] == 0) { // hasn't been chosen yet? 00035 L[x] = 1; // mark it "used" 00036 M[i] = x; // save result 00037 i = i + 1; // increment control variable 00038 } 00039 } 00040 Return(M); 00041 } 00042 00043 Static Void para1() { 00044 /***************************************************************************** 00045 Note that the second loop keeps repeating over and over until Random() has finally selected 00046 all N pitch classes. It then returns the newly created 12-tone row in M. 00047 *****************************************************************************/ 00048 Print("*** Random Row ***"); 00049 Print("A random row:", randomRow(12)); 00050 00051 /***************************************************************************** 00052 Here is an example row created by randomRow(): 00053 00054 {0, 6, 2, 9, 7, 5, 4, 10, 8, 3, 1, 11}; 00055 00056 Every pitch class is represented exactly once. 00057 *****************************************************************************/ 00058 } 00059 00061 /* $Revision: 1.3 $ $Date: 2006/09/05 08:02:46 $ $Author: dgl $ $Name: $ $Id: C091204a.cpp,v 1.3 2006/09/05 08:02:46 dgl Exp $ */ 00062 // The Musimat Tutorial © 2006 Gareth Loy 00063 // Derived from Chapter 9 and Appendix B of "Musimathics Vol. 1" © 2006 Gareth Loy 00064 // and published exclusively by The MIT Press. 00065 // This program is released WITHOUT ANY WARRANTY; without even the implied 00066 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00067 // For information on usage and redistribution, and for a DISCLAIMER OF ALL 00068 // WARRANTIES, see the file, "LICENSE.txt," in this distribution. 00069 // "Musimathics" is available here: http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10916 00070 // Gareth Loy's Musimathics website: http://www.musimathics.com/ 00071 // The Musimat website: http://www.musimat.com/ 00072 // This program is released under the terms of the GNU General Public License 00073 // available here: http://www.gnu.org/licenses/gpl.txt 00074 00075