ah my bad , i think this is probably the same as fisher yates;)
On Fri, May 7, 2010 at 12:20 PM, Hans Wichman <
[email protected]> wrote:
> Hi,
>
> here an alternative way of producing a random array:
>
> /**
> * Randomizes a copy of this array.
> */
> public function randomize (pArray:Array):Array {
> var newArray:Array = pArray.slice();
>
> var i:Number = newArray.length;
> while (i) {
> var p:Number = random(i);
> var t:Object = newArray[--i];
> newArray[i] = newArray[p];
> newArray[p] = t;
> }
>
> return newArray;
> }
> regards
> JC
> On Thu, May 6, 2010 at 3:27 AM, Juan Pablo Califano <
> [email protected]> wrote:
>
>> A simple way:
>>
>> Put all the candidate numbers in a list (in this case, 1 to 40). Then pick
>> randomly from that array, one at the time, and make sure you remove that
>> number from the candidates list, so you can't have duplicates.
>>
>> In code (untested):
>>
>> function getRandomList():Array {
>> var min:Number = 1;
>> var max:Number = 40;
>> var numItems:Number = 10;
>> var candidates:Array = [];
>> // fill up the candidates list with the "eligible" numbers
>> for(var i:Number = min; i <= max; i++) {
>> candidates.push(i);
>> }
>>
>> var list:Array = [];
>> var idx:Number = 0;
>> var selectedNumber:Number = 0;
>> for(i = 0; i < numItems; i++) {
>> // get a number from the candidates list, randomly. Add it to the
>> result and remove it from the candidates list (using splice)
>> idx = Math.floor(Math.random() * candidates.length);
>> selectedNumber = candidates.splice(idx,1)[0];
>> list.push(selectedNumber);
>> }
>> return list;
>> }
>>
>>
>> Cheers
>> Juan Pablo Califano
>>
>> 2010/5/5 Alan Neilsen <[email protected]>
>>
>> > 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
>> > [email protected]
>> > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>> >
>> _______________________________________________
>> Flashcoders mailing list
>> [email protected]
>> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>>
>
>
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders