Question
Generating non-repeating random numbers
I am creating a quiz. I have questions in one array, answers in another and want to create a set of non-repeating random numbers
At the moment I have seven questions (0-6) hence the limit in random number generation.
I have created the first random number and stored it along with the first question and answer in an array.
questionTest = the question array
randValue = the random number array
randomQ = the random number
Now I want to generate an array of non-repeating random numbers to generate further questions and answers.
This is one of many attempts. (I've taken out the trace statements). I know this doesn't work, how can I fix it?
Or is there a better approach?
while(q < questionTest.length-2) // I have already generated one number
{
while (randValue.length < questionTest.length)
{ q++;
//generate and test new random number
randomQ2 = Math.floor((Math.random()*7) + 0);
for (var r:int=0; r<randValue.length;r++)
{
if (randomQ2 == randValue
{ randomQ2 = Math.floor((Math.random()*7) + 0);}
if (!(randomQ2 == randValue
{ randomQ = randomQ2; }
}
randValue.push(randomQ);
}
}
The final output this time was => randValue = 3,1,5,4,5,0,6
I get one duplication and one missing number.