06 julio 2013

Reordenar aleatoriamente un vector Javascript

Esta función sirve para reordenar los elementos de un array de forma aleatoria:

Array.prototype.shuffle = function() {
    for ( var i = this.length-1; i ]]
>
 0; i-- ) {
        var j = Math.floor( i * Math.random() );
        var tmp = this[ j ];
        this[ j ] = this[ i ];
        this[ i ] = tmp;
    }
    return this;
}

No hay comentarios:

Publicar un comentario