Hi Allen,
Just a thought, but maybe have the quiz numbers set in an array that the var n:Number is retrieved and have the min and max that the random code calls match the amount of items in the array. After selecting a question, remove it from the array, thus the array length (max) reduces and the random code still works, but the previous questions are no longer in the array to choose from.

var numArray:Array = new Array();
numArray = [40, 20, 35, 4, 5, 9];
var currentNum:Number = null;

function randRange(min:Number, max:Number):Number {
    var randomNum:Number = Math.floor(Math.random(max,  min));
    return randomNum;
}

for (var i = 0; i < numArray.length; i++) {
    var n:Number = randRange(0, numArray.length);
    currentNum = numArray[n];
    numArray = numArray.splice(n);
    trace("incrament number: "+i);
    trace("currentNum: "+currentNum);
    trace("numArray: "+numArray);
}

This was off the top of the head, so you will need to test it.
HTH,

Karl

On May 5, 2010, at 7:45 PM, 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.

function randRange(min:Number, max:Number):Number {
var randomNum:Number = Math.floor(Math.random() * (max - min + 1)) + min;
    return randomNum;
}
for (var i = 0; i < 10; i++) {
    var n:Number = randRange(1, 40)
    trace(n);
}

When I run this it outputs a list of numbers like 40 13 17 12 27 12 3 17 9 15 which means some questions (in this case 17 and 12) will appear twice in my quiz. Bearing in mind that I am a bit of an ActionScript dummy, can anybody suggest a way to modify the above script to prevent the same number being generated more than once.

This message is for the named person’s use only. It may contain
confidential, proprietary or legally privileged information. No
confidentiality or privilege is waived or; lost by any mistransmission. If you receive this message in error, please immediately delete it and all copies of it from your system, destroy any hard copies of it and notify
the sender. You must not directly or indirectly, use, disclose,
distribute, print or copy any part of this message if you are not the
intended recipient. GOULBURN OVENS INSTITUTE OF TAFE and
any of its subsidiaries each reserve the right to monitor all e-mail
communications through its networks. Any views expressed in this
message are those of the individual sender, except where the
message states otherwise and the sender is authorised to state them
to be the views of any such entity.

###################################################################### ############### This e-mail message has been scanned for Viruses and Content and cleared
by MailMarshal
###################################################################### ###############
_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Karl DeSaulniers
Design Drumm
http://designdrumm.com


_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to