jamesbeith
February 27th, 2009, 12:18 PM
I have an XML file that is loaded in to Flash. Then from those ten questions in the XML file I want to randomly load 8 of them in to an array and ensure none of them are duplicates. It all works apart from I am getting duplicate questions put in to the array. Please help me solve this snippet of the code below so that the random number get regenerated if that related question is already in the array.
///// START QUESTION SELECTION /////
// new variables for questions functions
var numQuestions:int = 8; // number of questions to include in the quiz from the XML file
var questionArray:Array = new Array(numQuestions);
var i:int;
var j:int;
var RandNum:int;
var RandNum1:int;
// questions array function
function pickQuestions(TypeInput:XML):void {
for(i = 0; i < numQuestions; i++) {
questionArray[i] = new Array(3); // create three columns for each row
do {
RandNum = Math.random()*xmlData.Type.length(); // pick a new random number from length of XML file
} while(checker(RandNum) == -1)
questionArray[i][0] = TypeInput.Type.ID.text()[RandNum];
questionArray[i][1] = TypeInput.Type.Question.text()[RandNum];
questionArray[i][2] = TypeInput.Type.Answer.text()[RandNum];
// trace array
// trace("QUESTION NUMBER: " + i);
trace("XML ID: " + questionArray[i][0]);
// trace(questionArray[i][1]);
// trace(questionArray[i][2]);
trace("");
}
}
// function to ensure unique random questions
function checker(RandNum1) {
for (j = 0; j < questionArray.length; j++) {
if (RandNum1 == questionArray[i][0]) {
return (-1);
}
}
return (RandNum1);
}
///// END QUESTION SELECTION /////
///// START QUESTION SELECTION /////
// new variables for questions functions
var numQuestions:int = 8; // number of questions to include in the quiz from the XML file
var questionArray:Array = new Array(numQuestions);
var i:int;
var j:int;
var RandNum:int;
var RandNum1:int;
// questions array function
function pickQuestions(TypeInput:XML):void {
for(i = 0; i < numQuestions; i++) {
questionArray[i] = new Array(3); // create three columns for each row
do {
RandNum = Math.random()*xmlData.Type.length(); // pick a new random number from length of XML file
} while(checker(RandNum) == -1)
questionArray[i][0] = TypeInput.Type.ID.text()[RandNum];
questionArray[i][1] = TypeInput.Type.Question.text()[RandNum];
questionArray[i][2] = TypeInput.Type.Answer.text()[RandNum];
// trace array
// trace("QUESTION NUMBER: " + i);
trace("XML ID: " + questionArray[i][0]);
// trace(questionArray[i][1]);
// trace(questionArray[i][2]);
trace("");
}
}
// function to ensure unique random questions
function checker(RandNum1) {
for (j = 0; j < questionArray.length; j++) {
if (RandNum1 == questionArray[i][0]) {
return (-1);
}
}
return (RandNum1);
}
///// END QUESTION SELECTION /////