Question

I am creating a FlashCS4 Application that is in the style of a quiz. The questions are stored in a seperate text file and are called into the program through as3. This all works fine, however I am wondering how to randomise this data, but to make sure that the same question is not pulled twice.

For example, at the moment when I navigate to the questions page, I can display each part of the question (answer a,b,c,d + the question itself) and then this can be proceeded through 10times.

What I am trying to do is make these 10questions be randomly generated from the (27?) questions that I have in the text file.

    import flash.geom.Transform;
    import flash.geom.ColorTransform;
    import fl.motion.Color;


var glowFilter:GlowFilter = new GlowFilter(0x000000, 1, 2, 2, 10, 3)
var questionNumber:int = 0;
var totalCorrect:int = 0;
var selectedAnswer:String;
var checkAnswer:String;
var correctAnswer:String;
var questionCount:int = 0;
var numberOfQuestions:int = 10;
txt_loggedin_Question.text = (userName);

//Displays the Question Number which is called in from XML
txt_QuestionNumber.text = ("Question #"+questions[questionNumber].ref +" of"+numberOfQuestions);

function CheckAnswer() {
if (selectedAnswer == correctAnswer){
    totalCorrect = totalCorrect + 1;
    trace("Correct");
}else{
        totalCorrect = totalCorrect;
        trace("incorrect");
    }

            questionNumber = questionNumber + 1;    
            questionCount = questionCount + 1;  


    //Random questions set up new variable questioncount
    if (questionCount == numberOfQuestions){
        trace("we are at 10");
        gotoAndStop (1, "Result");
        //STOP RUN NEXT SCENE
    }else{
        setUpQuestions()
    }

There is a fair bit of code missing, but I am hoping that this covers the essentials, the file is called on a seperate page,

var questions:Array = [ ];

var request:URLRequest = new URLRequest("1.txt");
var loader:URLLoader = new URLLoader(request);

loader.addEventListener(Event.COMPLETE, completeHandler);

function completeHandler(event:Event):void
{
// loader data - the questions.txt file
var data:String = event.target.data;
// split data by newline for each question
var lines:Array = data.split("\n");

// for every line
for each (var line:String in lines)
{
    // split line by "||" delimiter
    var question:Array = line.split("||");

    // add the question to the questions array:
    questions.push({ref: question[0],
                    question: question[1],
                    answerA: question[2],
                    answerB: question[3],
                    answerC: question[4],
                    answerD: question[5],
                    answerE: question[6],
                    correct: question[7],
                    answer: question[8],
                    type: question[9],
                    file: question[10]});
}

}

All of this works but the only thing I am struggling with is to randomly generate the questions from within the text file each time that the scene is loaded. Sorry for the long winded question.

Thanks for reading.

Was it helpful?

Solution

Here are the steps you need to do:

Get your list of questions as an array called "orderedQuestions".

Create a second empty array called "shuffledQuestions".

Create while loop:

while(orderedQuestions.length > 0){
     shuffledQuestions.push(orderedQuestions.splice(Math.floor(Math.random()*orderedQuestions.length),1));
}

The loop removes one question at random from the sorted list and adds it to the shuffled list. When done your sorted list will be empty and the shuffledQuestions will have all of the questions added in a random order.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top