Saturday, February 15, 2014

Random Row Generator

For the longest time I've considered myself computer illiterate. Not in the sense that the term has conventionally been used (e.g., for grannies just learning how to use the mouse), but rather in the sense that the term must be used in a computer-saturated society, one in which the bar for literacy has been raised. Maybe it's optimistic to say this, but I think that “computer illiteracy” these days implies ignorance of programming and other higher level tasks, rather than a total unfamiliarity with the operation of a computer.

In an attempt to remedy my own illiteracy I recently started working through a couple of the courses at Codecademy, which is kind of like a Duolingo for computer languages. Specifically I've done about a third of the JavaScript course, and I've used what I've learned so far to build a random twelve-tone row generator. It is of course little more than a number scrambler (the digits 0–11)—and I'm absolutely sure that its code is inelegant, that there are more efficient ways (to which I'm currently ignorant) of coding its task (I'm thinking particularly of my having to input code for each note, which I doubt is really necessary)—but still it's kind of a cool little thing that some may find useful. In the future I hope to code some tools of more uniquely musical utility (maybe to uncover contrapuntal possibilities between pitch-class sets). Anyway, click the button below to generate a random twelve-tone row:



2/16: After sharing this post with the good people at r/MusicTheory, Reddit's music theory community, a couple users suggested some more efficient ways of generating a random twelve-tone row. Check out what they have to say, and thanks to u/kilimanjaroxyz and u/BinaryBullet for their suggestions! The latter also realized his code in relation to another *.js file that allows you to hear your row. Check it out on Plunker.

Here is [my original] code if anyone is interested:

function randomRowGenerator() {
   
confirm("Please click OK to generate a random twelve-tone row.");

var randomRow = [100,101,102,103,104,105,106,107,108,109,110,111];

for (y = 0; y < 12; y++) {

while(randomRow[y] > 99) {
x = Math.random()

//0//
var noteCount = 0
for (i = 0; i < y; i++) {       
        if (randomRow[i] == 0) {
            noteCount = (noteCount + 1)
        }       
    }
if ((noteCount == 0) && (x <= 0.083)) {
        randomRow[y] = 0
    }

//1//
noteCount = 0
for (i = 0; i < y; i++) {       
        if (randomRow[i] == 1) {
            noteCount = (noteCount + 1)
        }       
    }
if ((noteCount == 0) && (x > 0.083) && (x <= 0.166)) {
        randomRow[y] = 1
    }
   
//2//
noteCount = 0
for (i = 0; i < y; i++) {       
        if (randomRow[i] == 2) {
            noteCount = (noteCount + 1)
        }       
    }
if ((noteCount == 0) && (x > 0.166) && (x <= 0.249)) {
        randomRow[y] = 2
    }
   
//3//
noteCount = 0
for (i = 0; i < y; i++) {       
        if (randomRow[i] == 3) {
            noteCount = (noteCount + 1)
        }       
    }
if ((noteCount == 0) && (x > 0.249) && (x <= 0.332)) {
        randomRow[y] = 3
    }
   
//4//
noteCount = 0
for (i = 0; i < y; i++) {       
        if (randomRow[i] == 4) {
            noteCount = (noteCount + 1)
        }       
    }
if ((noteCount == 0) && (x > 0.332) && (x <= 0.415)) {
        randomRow[y] = 4
    }
   
//5//
noteCount = 0
for (i = 0; i < y; i++) {       
        if (randomRow[i] == 5) {
            noteCount = (noteCount + 1)
        }       
    }
if ((noteCount == 0) && (x > 0.415) && (x <= 0.499)) {
        randomRow[y] = 5
    }
   
//6//
noteCount = 0
for (i = 0; i < y; i++) {       
        if (randomRow[i] == 6) {
            noteCount = (noteCount + 1)
        }       
    }
if ((noteCount == 0) && (x > 0.499) && (x <= 0.582)) {
        randomRow[y] = 6
    }
   
//7//
noteCount = 0
for (i = 0; i < y; i++) {       
        if (randomRow[i] == 7) {
            noteCount = (noteCount + 1)
        }       
    }
if ((noteCount == 0) && (x > 0.582) && (x <= 0.665)) {
        randomRow[y] = 7
    }
   
//8//
noteCount = 0
for (i = 0; i < y; i++) {       
        if (randomRow[i] == 8) {
            noteCount = (noteCount + 1)
        }       
    }
if ((noteCount == 0) && (x > 0.665) && (x <= 0.748)) {
        randomRow[y] = 8
    }
   
//9//
noteCount = 0
for (i = 0; i < y; i++) {       
        if (randomRow[i] == 9) {
            noteCount = (noteCount + 1)
        }       
    }
if ((noteCount == 0) && (x > 0.748) && (x <= 0.831)) {
        randomRow[y] = 9
    }
   
//10//
noteCount = 0
for (i = 0; i < y; i++) {       
        if (randomRow[i] == 10) {
            noteCount = (noteCount + 1)
        }       
    }
if ((noteCount == 0) && (x > 0.831) && (x <= 0.914)) {
        randomRow[y] = 10
    }
   
//11//
noteCount = 0
for (i = 0; i < y; i++) {       
        if (randomRow[i] == 11) {
            noteCount = (noteCount + 1)
        }       
    }
if ((noteCount == 0) && (x > 0.914)) {
        randomRow[y] = 11
    }

}

}

alert("Your random twelve-tone row (in integer notation) is: " + randomRow);

}

No comments:

Post a Comment

Feel free to comment (anonymously if you want), just keep it civil/tasteful.