Tuesday, September 15, 2015

creating random rhythms

/*

Public notes: This program generates rhythmic note lengths with 1-9 beat durations. The order is randomly generated.  The note durations do not repeat.

Personal Notes: I will code a program which generates a random pattern of K*** and R*** k.  K represents the note, R represents a rest. I want this program to generate in a random pattern of all the nojte durations that could occur. But it will only give them once each time it is run, not more than once.

Here is my strategy, first I will set variables.
The string will be stored in a variable called rhythm.
rhythm will be set as the first value. then concated with additional randomly generated values.In between the concatenation there will be an "K" concatenated as well.   This will be the first program.

NExt program will take the random value generated and store it in a variable before concatenating with rhythm.  Whatever the value of the variable, there will be a designated variable created to confirm that the value has not already been concatenated with rhythm.  So the program will set the value equal to the randomly generated value, if the value is equal to for example 5, check if the variable five = 5 and if it does, start loop over again. if the variable five = null, then set variable five = to 5, concatenate to rhythm and then run the loop over again.  Set a counter so that the loop does not time out when it hits all the values.
*/



// Here is version two... in progress.

var zero = null;
var one = null;
var two = null;
var three = null;
var four = null;
var five = null;
var six = null;
var seven = null;
var eight = null;
var rhythm =  "";
var lights = null;

for (var counter = 0; counter < 9;)
{
random = Math.round(Math.random() * 8);
var lights = 1;
if (random === 0)
 {if (zero === 0)
  lights = 0;
     else
     {zero = 0;
      counter++;
     }
        }
else if (random === 1)
 {if (one === 1)
 lights = 0;
     else
     {one = 1;
     counter++;
     }
        }
else if (random === 2)
 {if (two === 2)
 lights = 0;
     else
     {two = 2;
     counter++;
     }
        }
else if (random === 3)
 {if (three === 3)
 lights = 0;
     else
     {three = 3;
      counter++;
     }
        }
else if (random === 4)
 {if (four === 4)
lights = 0;
     else
     {four = 4;
      counter++;
     }
        }
else if (random === 5)
 {if (five === 5)
        lights = 0;
     else
     {five = 5;
      counter++;
     }
        }
else if (random === 6)
 {if (six === 6)
        lights = 0;
     else
     {six = 6;
      counter++;
     }
        }
else if (random === 7)
 {if (seven === 7)
        lights = 0;
     else
     {seven = 7;
      counter++;
     }
        }
else if (random === 8)
 {if (eight === 8)
        lights = 0;
     else
     {eight = 8;
      counter++;
     }
        }
if  (lights ===1)
 {rhythm += "R";
     for (var counter3 = 0; counter3 < random; ++counter3)
    rhythm += "*";
 }
}
console.log(rhythm);

No comments:

Post a Comment