[AS 1 or 2] array.splice() fun.
These do the same thing but in different ways. #2 is a bit faster since there is no number iteration. These were made to pick a given amount of items (at random) from an array. Each item is shown only once. These do kill the array, so making a copy first would be a good idea if you need to use it more than once.
/* Ask Some Questions!!! */
questions = ["q1", "q2", "q3", "q4", "q5", "q6", "q7", "q8", "q9", "q10",
"q11", "q12", "q13", "q14", "q15", "q16", "q17", "q18", "q19", "q20"];
askNumber = 15; // how many "questions" to ask.
for(i = 0; i < askNumber; i++){
rNum = Math.floor(Math.random()*(questions.length));
trace(questions[rNum]);
questions.splice(rNum,1);
}
/* Hold Back Some Questions!!! */
questions = ["q1", "q2", "q3", "q4", "q5", "q6", "q7", "q8", "q9", "q10",
"q11", "q12", "q13", "q14", "q15", "q16", "q17", "q18", "q19", "q20"];
holdNumber = 10; // How many "questions" to hold back.
while(questions.length>holdNumber){
rNum = Math.floor(Math.random()*(questions.length));
trace(questions[rNum]);
questions.splice(rNum,1);
}
/* Remove Like Array Elements */
function removeLikeElements(_array, _element){
var i = _array.length;
while (i--) if (_array[i] == _element) _array.splice(i,1);
}
Thanks goes to
Senocular the 8 eyed wonder from the
Kirupa Forums for that last one. (his site has some great FireWorks exensions, flash examples, and tons of other stuff.. plus he is a nice guy, so check it out).