- Main Page
- Namespaces
- Files

- File List
- File Members

# C:/Musimathics_local/Musimat/MusimatChapter9/C091204a.cpp

Go to the documentation of this file.

    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) {L[i] = 0;}
    00027
    00028         // build up M, marking off elements in L when they are chosen
    00029         i = 0;
    00030         While (i < N) {
    00031                 Integer x = Random(0, N - 1);           // returns integer random value
    00032                 If (L[x] == 0) {                                        // hasn’t been chosen yet?
    00033                          L[x] = 1;                                              // mark it "used"
    00034                          M[i] = x;                                              // save result
    00035                          i = i + 1;                                     // increment control variable
    00036                 }
    00037         }
    00038         Return(M);
    00039 }
    00040
    00041 Static Void para1() {
    00042 /*****************************************************************************
    00043 Note that the second loop keeps repeating over and over until Random() has finally selected
    00044 all N pitch classes. It then returns the newly created 12-tone row in M.
    00045 *****************************************************************************/
    00046         Print("*** Random Row ***");
    00047         Print("A random row:", randomRow(12));
    00048
    00049 /*****************************************************************************
    00050 Here is an example row created by randomRow():
    00051
    00052 {0, 6, 2, 9, 7, 5, 4, 10, 8, 3, 1, 11};
    00053
    00054 Every pitch class is represented exactly once.
    00055 *****************************************************************************/
    00056 }}
    00057
    00059 /* $Revision: 1.2 $ $Date: 2006/09/09 06:22:43 $ $Author: dgl $ $Name:  $ $Id: _c091204a_8cpp-source.html,v 1.2 2006/09/09 06:22:43 dgl Exp $ */
    00060 // The Musimat Tutorial © 2006 Gareth Loy
    00061 // Derived from Chapter 9 and Appendix B of "Musimathics Vol. 1" © 2006 Gareth Loy
    00062 // and published exclusively by The MIT Press.
    00063 // This program is released WITHOUT ANY WARRANTY; without even the implied
    00064 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00065 // For information on usage and redistribution, and for a DISCLAIMER OF ALL
    00066 // WARRANTIES, see the file, "LICENSE.txt," in this distribution.
    00067 // "Musimathics" is available here:     http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10916
    00068 // Gareth Loy's Musimathics website:    http://www.musimathics.com/
    00069 // The Musimat website:                 http://www.musimat.com/
    00070 // This program is released under the terms of the GNU General Public License
    00071 // available here:                      http://www.gnu.org/licenses/gpl.txt
    00072
    00073