PDA

View Full Version : Shuffle Arrays



dilpreet
June 8th, 2009, 01:54 AM
Hey i have a quiz with 20 questions and all of its answers in 2 different arrays. I want to know how i can make those 20 questions and the all of the answers appear in a random order when anyone takes the quiz.

Thanks for any replies.

.ral:cr
June 8th, 2009, 02:20 AM
pretty impossible, try to keep both into an object
[{question:"", answer:""}, ....]

dilpreet
June 8th, 2009, 02:25 AM
what do u mean by impossible. how about just shuffling just questions into a random order

JonnyR
June 8th, 2009, 03:14 AM
Dilpreet,

I think what .ral:cr is trying to say is that your choice of Data Structure is non standard. The preffered way to solve the "Quiz" problem is to create a single Array which contains QuizQuestion Objects. These QuizQuestion objects would have question and answer properties which would be displayed on screen.

However, to answer your original question - instead of shuffling the arrays, you should instead randomise the index value that you are going to read from, eg:



var questions : Array = [];
questions.push("What colour is the sky?");
questions.push("What do chickens lay?");
questions.push("What is 12 - 10?");

var answers : Array = [];
answers.push("Blue");
answers.push("Eggs");
answers.push("2");

// Get the number of Questions avaliable.
var numQuestions : int = questions.length;

// Pick a random question (note that Array's start at Index 0, that's
// why we subtract 1 from numQuestions).
var currentQuestionIndex : int = Math.ceil(Math.random() * (numQuestions - 1));

// Trace out the current Question and Answer.
var currentQuestion : String = questions[currentQuestionIndex];
var currentAnswer : String = answers[currentQuestionIndex];
trace("Question " + currentQuestionIndex + ", " + currentQuestion + ". -- Answer: " + currentAnswer);


Jonny.

dilpreet
June 8th, 2009, 07:33 PM
Hey Jonny

ur code works great but just with one question. I want all the questions but in a random order

thanks

creatify
June 8th, 2009, 10:20 PM
// same as above basically, but utilizes an indice array, shuffles that, then your q/a arrays stay in order - one way to do this anyhow
var indices:Array = [0,1,2,3,4,5,6,7,8,9,10,11];
var ques:Array = ["Q0","Q1","Q2","Q3","Q4","Q5","Q6","Q7","Q8","Q9","Q10","Q11"];
var answ:Array = ["A0","A1","A2","A3","A4","A5","A6","A7","A8","A9","A10","A11"];

function shuffle(a:*,b:*):int {
return int(Math.round(Math.random()*2)-1);
}
trace(indices);
var indicesMix:Array = indices.sort(shuffle);
trace(indicesMix);

var len:int = indicesMix.length;
for (var i:int=0; i<len; i++) {
trace(ques[indicesMix[i]]+": "+answ[indicesMix[i]]);
}

// so you won't access ques and answ via ques[0] and answ[0], you'll access via ques[indicesMix[0]] and answ[indicesMix[0]];

Shaedo
June 9th, 2009, 01:40 AM
The way that creatify does it is probably better than mine (I am only fairly new at this) but the way I solve this is to create a 2D matrix (array of arrays) and have the question in say row one and the answer in row 2. That way if you shuffle the array (say called QnAarray) the Q and A are always together in the same column. You can also add another row for keeping track of say whether a question was answered (for adding a pass option), was answerd correctly etc...

so in your case your code might be something along the lines of this:


for(var i:uint=0;i<questionArray.length;i++)
{
QnAarray[i] = new Array;//creates an array in an array
QnAarray[i][0] = questionArray[i];//adds questions in order in column 0
QnAarray[i][1] = answerArray[i];//adds answers in order in column 1
}

you can then shuffle this array using code like this:


function randomizeArray(array:Array):Array
{//from http://snipplr.com/view/11307/as3-randomize-array/
var tempArray:Array = new Array();
while(array.length > 0){
tempArray.push(array.splice(Math.floor(Math.random ()*array.length), 1));
}
return tempArray;
}

then to call Q and As like creatify did you code would be somthing like this:

for(var i:int=0; i<QnAarray.length; i++) {
trace("Question" + QnAarray[i][0] + " Answer: "+ QnAarray[i][1]);
}

As and aside, I tend not to shuffle my Q&A array but rather create a seperate array of numbers from 1 to the length of the Q&A array and shuffle that instead. This is probably not applicable (and I appologise for any time wasted) but it does give the advantage of meaning you always have an intact Q&A array and you can change what your Q&A is (eg text, object, MC etc) very easily and you can make what ever post shuffle modifications you want to your array (eg knock out the corresponding number as the questions are answered) without touching your QandA array.

Best of luck!