Your random number generation is not only mathematically wrong (i.e. it won't generate truly random items), it will also from time to time generate a number that is beyond the array's bounds. To explain this: 1+(array.length-1) * Math.random()
will generate any number greater or equal to 1 (this will also result in the first item of the array never to be returned, because arrays are 0-based), up to a fraction less than the actual length of the array. If you Math.round()
the highest possible result, it will round up to the next highest integer, which is the full length again - and if you access array[array.length]
, an error is thrown, which is probably responsible for the weird behavior you are seeing.
Here's a possible solution:
Math.round()
creates random number bias, anyway (see @OmerHassans link), so you're better off using int()
or Math.floor()
. Also, Math.random()
is defined as 0 <= n < 1
, so it will never return 1. Therefore, you can simplify your random index generator to int(Math.random()*array.length)
=> any integer smaller than the length of the array.
splice()
, then, returns an array of the items that were removed, so you can pass its first item, instead of creating a new array.
function getRandomItem( arr:Array ):* {
var rand:int = Math.random()*arr.length;
return arr.splice( rand, 1 )[0];
}
function makeQuestion():void {
var q:Array = getRandomItem( quizQuestions );
mc_quiz.question.text = q[0];
answer=q[1];
printQuestions[printQuestions.length] = q;
}
FYI: It won't matter much in this context, but you get much faster performance by replacing array.push(item)
with array[array.length]=item;
.