/** * Generates an array of random numbers from 1 or 0 to max where all numbers through max are used, and no number is zero (if specified). * Specify true if no zeros are to be included, specify false if zeros are to be included in the resulting array. * @param max the maximum range for the numbers. * @param useZero Specify true if no zeros are to be included, specify false if zeros are to be included in the resulting array. * @return an array of random numbers where no number is the same. * */ public static function rndNumArray(max:int, useZero:Boolean=false):Array { var array:Array = []; for (var i1:int = 0; i1 < max; i1++) { array.push(""); } var i2:int = 0; while (i2 < array.length) { var rndNum:Number = rndNum(0, max); if (!containedInArray(rndNum, array) ) { //if (useZero) rndNum = rndNum-1; array[i2] = rndNum; i2++; } } if (useZero) { var arrLen:int = array.length; for (var i3:int = 0; i3 < arrLen; i3++) { array[i3] = array[i3]-1; } } return array; }
/** * Generates a random number within the given range. * * @example <listing version=<3.0"> * var myRandomNumber:Number = rndNum(2, 13);<br/> * trace(myRandomNumber); //returns 7 (a random between 2 and 13) * </listing> * @param minVal the minimum number for the range. * @param maxVal the maxiumum number for the range. * @return returns the random number generated. * * */ public static function rndNum(minVal:Number, maxVal:Number):Number { return minVal + Math.floor(Math.random()*(maxVal+1-minVal)); } Jason Merrill Bank of America Global Learning Learning & Performance Solutions Join the Bank of America Flash Platform Community and visit our Instructional Technology Design Blog (note: these are for Bank of America employees only) -----Original Message----- From: flashcoders-boun...@chattyfig.figleaf.com [mailto:flashcoders-boun...@chattyfig.figleaf.com] On Behalf Of kennethkawam...@gmail.com Sent: Thursday, May 06, 2010 5:03 AM To: Flash Coders List Subject: Re: [Flashcoders] Producing a random list with no repeats I always use Fisher-Yates shuffle method to randomise an Array, which yields more unbiased result. http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle My AS3 interpretation of Fisher-Yates is as follows; I can just call this from anywhere in my scripts ;) package utils { public function fisherYates(arr:Array):void { var i:uint = arr.length; while(--i){ var j:uint = Math.floor(Math.random()*(i + 1)); var tmpI:Object = arr[i]; var tmpJ:Object = arr[j]; arr[i] = tmpJ; arr[j] = tmpI; } } } -- Kenneth Kawamoto http://www.materiaprima.co.uk/ On 6 May 2010 02:27, Juan Pablo Califano <califa010.flashcod...@gmail.com> 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 <aneil...@gotafe.vic.edu.au> > >> 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. _______________________________________________ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders _______________________________________________ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders