Posted on

How to make the fastest random array in AS3 CS4

There are some of my research like this and this. Base on these research, I come up a ” randomized square” or ” randomized matrix”.

Speaking of random array, I did some comparison on different functions sets and evaluated their efficiency. Here is the code to exam each type of method.

Array.prototype.mixElements = function()
{
var _length:Number = this.length, rn:Number, it:Number, el:Object;
for (it = 0; it < _length; it++)
{
el = this[it];
this[it] = this[rn = random(_length)];
this[rn] = el;
}
};

function mixArray(array:Array):Array
{
var _length:Number = array.length, mixed:Array = array.slice(), rn:Number, it:Number, el:Object;
for (it = 0; it 0)
{
brr.push(arr.splice(Math.floor(Math.random() * arr.length), 1)[0]);
}

return brr;
}

function shuffle(a, b):Number
{
var num:Number = Math.round(Math.random() * 2) - 1;
return num;
}
function mixArray_while(array:Array):Array
{
var _length:Number = array.length, mixed:Array = array.slice(), rn:Number, it:Number, el:Object;
while (it < _length)
{
el = mixed[it];
mixed[it] = mixed[rn = it + random(_length - it)];
mixed[rn] = el;
it++;
}
return mixed;
}
function createArray(arraySize:Number):Array
{
var array:Array = new Array();
for (i = 0; i < arraySize; i++)
{
array[i] = i;
}
return array;
}

var arraySize:Number = 1500;

/* metod 1 */
var a:Array = createArray(arraySize);
startTime = getTimer();
var b:Array = a.sort(shuffle);
numTimeElapsed = getTimer() - startTime;
trace("\nMethod 1, time: " + numTimeElapsed);
//trace(b);

/* metod 2 */
var a:Array = createArray(arraySize);
startTime = getTimer();
var b:Array = shuffle_by_splice(a);
numTimeElapsed = getTimer() - startTime;
trace("\nMethod 2, time: " + numTimeElapsed);
//trace(b);

/* metod 3 */
var a:Array = createArray(arraySize);
startTime = getTimer();
a.mixElements();
numTimeElapsed = getTimer() - startTime;
trace("\nMethod 3, time: " + numTimeElapsed);
//trace(a);

/* metod 4 */
var a:Array = createArray(arraySize);
startTime = getTimer();
var b:Array = mixArray(a);
numTimeElapsed = getTimer() - startTime;
trace("\nMethod 4, time: " + numTimeElapsed);
//===============================================================

/* metod 5 */
var a:Array = createArray(arraySize);
startTime = getTimer();
var b:Array = mixArray_while(a);
numTimeElapsed = getTimer() - startTime;
trace("\nMethod 5, time: " + numTimeElapsed);
trace("p.s. this method is according to ");
//=================================================================

Amazingly, I found this result in the output screen! Now I can choose which method to generate a 100 by 100 elements square structure is the fastest.

function mixArray_while(array:Array):Array
{
var _length:Number = array.length, mixed:Array = array.slice(), rn:Number, it:Number, el:Object;
while (it < _length)
{
el = mixed[it];
mixed[it] = mixed[rn = it + random(_length - it)];
mixed[rn] = el;
it++;
}
return mixed;
}
var a:Array = [1,2,3,4,5,6,7,8];
var b:Array = mixArray_while(a);
trace(b+"\n done!");

This result points out that using “while” loop in AS3 CS4 does terrific job while looping in “for” gives crappy result!

Advertisements

About Kemo

I am just an ordinary person in the garden.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s