Alan Neilsen wrote:
Thanks to all the suggestions re my random list problem. Particular thanks to 
Gerry Beauregard for this suggestion:

"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......"

The following method of shuffling an array works fine in AS2:

var numArray:Array = new Array();
numArray = 
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40];
shuffle(numArray);

function shuffle(arr:Array) {
      var i:Number;
      for (i in arr) {
            var tmp = arr[i];
            var rNr:Number = Math.floor(Math.random()*arr.length);
            arr[i] = arr[rNr];
            arr[rNr] = tmp;
      }
}


I doubt the quality of that algorithm. If you do some tests, it probably has a bias of some sort. You have forced a sorting algorithm to act as a randomization algorithm.

I prefer to just use the hat picking algorithm instead.
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to