10 Replies Latest reply on Dec 8, 2008 4:27 AM by Ned Murphy

# 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[r])
{ randomQ2 = Math.floor((Math.random()*7) + 0);}
if (!(randomQ2 == randValue[r]))
{ 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.
• ###### 1. Re: Generating non-repeating random numbers
I didn't try to decipher what you did... when I see a while loop I start to cringe (they can be dangerous). If you have X questions and X answers:

• ###### 2. Re: Generating non-repeating random numbers
that's not a good array randomizer because the first and last original array elements will tend to occur at the end of the randomized array more than you would expect if the original array were really randomized.

for the purposes of your quiz, nedwebs code might be good enough, but if you want to use something that's closer to randomizing your array you can use the following function to randomize any array:

• ###### 3. Generating non-repeating random numbers
I tried to locate kGlad's shuffle routine before I posted my version, but my search failed to find it... go with his. I guess I best copy it now for my own good.
• ###### 4. Re: Generating non-repeating random numbers
i have a flash/resource folder that i store code bits that i find useful. that's where i store the shuffle function.
• ###### 5. Re: Generating non-repeating random numbers
thanks for that

You don't know where I might find some code for a good analog clock timer? I've been trying to modify the code found at
context=LiveDocs_Parts&file=00000075.html
but haven't much experience with "public" functions
• ###### 6. Re: Generating non-repeating random numbers
[to kGlad] I've started doing the same thing... it's helpful when you continually see the same queries popping up.

[to fungus13] No, I haven't looked though. HAve you tried googling "Flash analog clock tutorial"
• ###### 7. Re: Generating non-repeating random numbers
Hi Ned
How do you think I found the code in the first place. It's controlling the clock out of standard time that is the problem. I may be able to figure it out, but implementing it in a "public" function is not so easy.
• ###### 8. Generating non-repeating random numbers
Hello... I am creating a quiz also. I have eight target containers, and I have eight small images (balloon, kite, etc...) wrapped in MCs.

The example here - the one I used - grabs 1 random number - which I used to load a MC into a container such as:

Code:

stop();
myRandom=random(3)+1;

I would like to do a few things beyond this. The most complex is instead of grabbing 1 random number of 3 I would like to grab 3 random numbers of 8 for loading three random image MCs. And I would like to grab three other random numbers of eight to define the random containers the random images will load into.

The stage is a grid of eight containers named 1 through 8. The main directory has eight MCs with images.

I want to load three different random movie clips over three seconds into three different random containers. Random clip 1 loads into a random container, one second later random clip 2 loads into a second random container (the first MC unloads as the second clip loads-up), one more second later random clip 3 loads into a random container. Then one more second later an enter frame clip event will reload the entire screen with a quiz MC.

So in short - three random clips are shown to the viewer over three seconds in three different containers - all random. Then they are presented with a screen that allows the user to think about what they saw for 8 seconds. After 8 seconds they are shown another MC that displays a random MC1 through 8 in a random container as the prompt. The viewer then selects a button - either YES or NO (Either the 4th random MC is one they actually saw (the balloon in the top right corner) during the three seconds of MCs they initially viewed or it was not (the balloon in the bottom left corner)). This information is sent to the db.

So what I am asking is this. If we can generate three random numbers from 1-8 to load the first three movies over three seconds into three random containers - and then can we compare the values of those image and container arrays with the value of the very last random numbers generated to load the 4th and last prompt quiz MC to determine if their is a match?

If their is are matches (a correct movie clip in the correct container) a YES would populate the db as "correct"! (ie, 1,4,7 compared to 4) If their is not a match a YES would populate the db as "incorrect" (ie, 1,4,7 compared to 2). If their is a match a NO would populate the db as "incorrect" (ie, 1,4,7 compared to 7). And lastly, if their is not a match a NO would populate the db as 'correct' (ie, 1,4,7 compared to 3).

Does my description make sense?
Can we find some help setting this up?

Thank you,
Odisey
• ###### 9. Re: Generating non-repeating random numbers
create an array of anything you want to randomize without repeats, apply the shuffle function and then access whatever number of objects you need.
• ###### 10. Re: Generating non-repeating random numbers
Based on the Adobe link you showed, I couldn't rightly tell how you might have found it, but if you do a Google search on those keywords I mentioned, a number of different tutorials are listed, and the Adobe one isn't on the first page. The second one mentions that it is customizable.