Shuffle array - Node.js Array

Node.js examples for Array:Shuffle

Description

Shuffle array

Demo Code

Array.prototype.shuffle = function() { 
    var arr = this, elm, inc, length = arr.length;
 
    while (length) {
        inc = Math.floor(Math.random() * length--);

        elm = arr[length];//from   w  w  w  .j  a  v a2  s  .  com
        arr[length] = arr[inc];
        arr[inc] = elm;
    }
 
    return arr; 
};

Related Tutorials