Ver Mensaje Individual
  #14 (permalink)  
Antiguo 15/10/2012, 05:36
Avatar de IsaBelM
IsaBelM
Colaborador
 
Fecha de Ingreso: junio-2008
Mensajes: 5.032
Antigüedad: 15 años, 10 meses
Puntos: 1012
Respuesta: Duda con shuffle

Cita:
Iniciado por marlanga Ver Mensaje
Más simple que el funcionamiento de un botijo, pero 100% rápido y fiable.
en qué te basas para decir eso?? mas elegante sí, fiable sí, pero rápido, .... veamoslo
Código:
var miArray1=["aaaaaaa","bbbbbbbbbbb","ccccccccccc","ddddddddddd"];
var miArray2=["aaaaaaa","bbbbbbbbbbb","ccccccccccc","ddddddddddd"];
var miArray3=["aaaaaaa","bbbbbbbbbbb","ccccccccccc","ddddddddddd"];




// MARLANGA

Array.prototype.mezclar= function() {
var ini = new Date().getTime();
    var n=this.length;
    while(n--)
    {
        var i=Math.floor(n*Math.random());
        var tmp =this[i];
        this[i]=this[n];
        this[n]=tmp;

    }
var fin = new Date().getTime();
var diff = fin - ini;
    return this + ' - ' + diff;
}
 
console.log(miArray1.mezclar());





// EMPREAR

var mezclar2 = function(arr){
var ini = new Date().getTime();
for(var i, tmp, n = arr.length; n; i = Math.floor(Math.random() * n), tmp = arr[--n], arr[n] = arr[i], arr[i] = tmp);
var fin = new Date().getTime();
var diff = fin - ini;
return arr + ' - ' + diff;
}

console.log(mezclar2(miArray2));







// MEZCLA MARLANGA/EMPREAR

Array.prototype.mezclar3 = function(){
var ini = new Date().getTime();
for(var i, tmp, n = this.length; n--; i = Math.floor(Math.random() * n), tmp = this[i], this[i] = this[n], this[n] = tmp);
var fin = new Date().getTime();
var diff = fin - ini;
return this + ' - ' + diff;
}

console.log(miArray3.mezclar3());
en definitiva. fiables los 3, elegante el de marlanga, rápido en aquellos que se usa el ciclo for por 1 milisegundo
__________________
if(ViolenciaDeGénero) {alert('MUJER ASESINADA');}