quote:
Originally posted by stu
I tried his method as well, but could not get that to work. If you think it can, that would be great if you could think of a way for me to figure it out, always better to know more than one method..
code:
var arrLyrics = Array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
var count = 0;
var arrUsedLyrics = new Array();
function GetIndex(arrArray, intRepeatAfter) {
var i = Math.round(Math.random()*arrArray.length);
for(var j=0;j<arrUsedLyrics.length;j++){
if(i==arrUsedLyrics[j]){
return GetIndex(arrArray,intRepeatAfter);
}
}
arrUsedLyrics[count]=i;
count++;
count=count%(intRepeatAfter-1);
return i;
}
I don't take credit for this code - foaly's idea.
and whenever you want to pick a random element from an array you just do:
var myRandomLyrics = arrLyrics[GetIndex(arrLyrics,10)];
Where 10 is the number of lyrics after one has been chose which must be chosen before that lyric can be repeated.
NOTE: You must still only ever have 1 array (as Cookie has pointed out) otherwise you will bump into problems.