as3 random array - randomize array - actionscript

2019-01-18 14:52发布

问题:

How do you randomize an array using actionscript 3?

回答1:

There is a short version using Array.sort() function:

var arr : Array = [0,1,2,3,4,5,6,7,8,9];

function randomize ( a : *, b : * ) : int {
    return ( Math.random() > .5 ) ? 1 : -1;
}

trace( arr.sort( randomize ) );

If you don't get "enough" randomness you can sort twice :)

EDIT - explanation line by line:

For Array class method sort() you can pass not only sort options like Array.CASEINSENSITIVE, Array.DESCENDING and so on but also your own custom compare function reference (a callback) that accepts two parameters (two elements from array to compare). From AS3 documentation:

A comparison function should take two arguments to compare. Given the elements A and B, the result of compareFunction can have a negative, 0, or positive value:

  • A negative return value specifies that A appears before B in the sorted sequence.
  • A return value of 0 specifies that A and B have the same sort order.
  • A positive return value specifies that A appears after B in the sorted sequence.

Note: compare function parameters might be typed (if your array is typed) and have any name you want eg.:

function compareElements ( elementA : SomeClass, elementB : SomeClass ) : int;

This method is very useful when you need to sort array elements by their special properties. In randomization case compareFunction randomly returns -1, 0 or 1 and makes array elements to switch their places (indices). I have found that better randomization (in my subjective and mathematically untested opinion) is when method returns only -1 and 1. Also have in mind that sorting function with custom compare function doesn't compare elements sequentially so in some special cases randomization results may differ from what you might expect.



回答2:

There's a better way that will also allow you to randomize the array in place, if you need that, and it will not make you create more then a single copy of your original array.

package
{
    import flash.display.Sprite;

    public class RandomizeArrayExample extends Sprite
    {
        public function RandomizeArrayExample()
        {
            super();
            testDistribution();
        }

        private function testDistribution():void
        {
            var hash:Object = { };
            var tester:Array = [1, 2, 3, 4];
            var key:String;

            for (var i:int; i < 1e5; i++)
            {
                randomize(tester);
                key = tester.join("");
                if (key in hash) hash[key]++;
                else hash[key] = 1;
            }
            for (var p:String in hash) trace(p, "=>", hash[p]);
        }

        private function randomize(array:Array):Array
        {
            var temp:Object;
            var tempOffset:int;
            for (var i:int = array.length - 1; i >= 0; i--)
            {
                tempOffset = Math.random() * i;
                temp = array[i];
                array[i] = array[tempOffset];
                array[tempOffset] = temp;
            }
            return array;
        }
    }
}


回答3:

I had an alternative requirement where i wanted to randomly insert lots of source arrays into a target array randomly. Like Rytis i'm a big fan of the forEach, map and sort functions on Arrays.

var randomInsert:Function = function callback(item:*, index:int, array:Vector.<MyItem>):void
{
    var j:Number = Math.floor(Math.random() * targetArray.length);
    targetArray.splice(j,0,item);                   
}

targetArray = new Vector.<MyItem>();
sourceArray1.forEach(randomInsert, this);
sourceArray2.forEach(randomInsert, this);


回答4:

here's an easier function. Works also on multidimensional arrays

function randomizeArray(array:Array):Array
{
    var newArray:Array = new Array();
    while (array.length > 0)
    {
        var mn=Math.floor(Math.random()*array.length)
        newArray[newArray.length]=array[mn]
        array.splice(mn,1)
    }
    return newArray;
}


回答5:

I found this very helpful. I hope it can help you too.

// Array to Randomize
var firstArray:Array = ["One","Two","Three","Four","Five","six","seven","eight","nine","ten"];
trace(firstArray); // Prints in order

var newArray:Array = new Array();
function randomizeArray(array:Array):Array
{
    var newArray:Array = new Array();

    while (array.length > 0)
    {
        newArray.push(array.splice(Math.floor(Math.random()*array.length), 1));
    }

    return newArray;
}

var randomArray:Array = randomizeArray(firstArray);
trace(randomArray); // Prints out randomized :)


回答6:

If you need your array to be shuffled (your elements can not repeat). You could use this function:

/**
 * Shuffles array into new array with no repeating elements. Simple swap algorithm is used.
 */
public function shuffleArray(original:Array):Array
{
    // How many swaps we will do
    // Increase this number for better results (more shuffled array, but slower performance)
    const runs:int = original.length * 3;
    var shuffled:Array = new Array(original.length);

    var i:int;
    var a:int;
    var b:int;
    var temp:Object;

    // Copy original array to shuffled
    for(i=0; i<shuffled.length; i++){
        shuffled[i] = original[i];
    }

    // Run random swap cycle 'runs' times
    for(i=0; i<runs; i++){
        // There is a chance that array element will swap with itself,
        // and there is always small probability it will make your shuffle
        // results not that good, hence try to experiment with
        // different runs count as stated above
        a = Math.floor(Math.random() * original.length);
        b = Math.floor(Math.random() * original.length);

        // Swap messages
        temp = shuffled[a];
        shuffled[a] = shuffled[b];
        shuffled[b] = temp;
    }

    return shuffled;
}

Usage:

var testArray:Array = ["Water", "Fire", "Air", "Earth"];
trace(shuffleArray(testArray).concat());


回答7:

this is how I randomize my array of 36 cards for a memory game

const QUANT_CARTAS: int = 36;

//get the 36 numbers into the array
for (var i: int = 0; i < QUANT_CARTAS; i++)
{
    cartas.push(i);
}

//shuffles them =)
for (var moeda: int = QUANT_CARTAS - 1; moeda > 0; moeda--)
{
    var pos: int = Math.floor(Math.random() * moeda);
    var carta: int = cartas[moeda];
    cartas[moeda] = cartas[pos];
    cartas[pos] = carta;
}
// and add them using the random order...

    for (i = 0; i < QUANT_CARTAS; i++)
{
    var novaCarta: Carta = new Carta();
    novaCarta.tipoCarta = cartas[i];
    etcetcetc.............
}


回答8:

choose random string from array

function keyGenerator(len:Number):String
{
    function randomRange(minNum:Number, maxNum:Number):Number
    {
        return (Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum);
    }
    var hexArray = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'];
    var key = "";
    for (var i=0; i<len; i++)
    {
        key +=  hexArray[randomRange(0,hexArray.length-1)];
    }
    return key;
}

usage:

trace(keyGenerator(16));