It's probably best just to create an Array or Vector containing numbers 1 to 
40.  (Vectors are only available in AS3, not sure about Array).  Shuffle the 
array, then choose the first 10 elements. A reasonably effective way to shuffle 
an array is to step through the array, and for each element swap it with some 
other randomly selected element. 

Another approach is to start with an array whose elements contain numbers 1 
through 40.  Randomly select one of the 40 elements, record its value, and 
remove it from the array.  Now you have 39 elements.  Randomly select one of 
the 39 elements, record its value, and remove it from the array.  Continue in 
this fashion until you've selected 10 elements.

The worst approach is to repeatedly generate random numbers in the range 1 
through 40, and add the number your list of questions only if it's not already 
on the list.  The reason this approach is bad is that the execution time is 
unpredictable - you could be 'rolling the dice' hundreds of times and always 
getting numbers that are already on your list.

On 2010-05-06  , at 08:45 , Alan Neilsen wrote:

> I am working in ActionScript 2. I want to create a quiz in which 10 questions 
> are randomly selected from a block of 40 questions. I found the following 
> code, but I can't work out how to stop it doubling up the questions.

Flashcoders mailing list

Reply via email to