[AS] Shuffle Function

I was bored so I saw how fast I could write a shuffle function in actionScript (PHP has one built in, but actionScript apparently doesn’t – at least not that I could find). It should randomly rearrange an array that you pass to it and return it.

Time: About 6 Minutes


myArray = new Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
trace(shuffle(myArray));
function shuffle(arrayIn) {
    arrayOut = new Array();
    size = arrayIn.length;
    var i = 0;
    while(i < size) {
        index = random(size);
        while(arrayOut[index] != undefined) {
            if(index == (size-1)) {
                index = 0;
            } else {
                index++;
            }
        }
        arrayOut[index] = arrayIn*;
        i++;
    }
    return arrayOut;
}