Use Random function to shuffle array - Node.js Array

Node.js examples for Array:Shuffle

Description

Use Random function to shuffle array

Demo Code

Array.prototype.shuffle = function () {

    var tmp, current, top = this.length;

    if (top) while(--top) {
        current = Math.floor(Math.random() * (top + 1));
        tmp = this[current];//w ww . j a  v  a  2  s  .  com
        this[current] = this[top];
        this[top] = tmp;
    }

    return this;
};

Related Tutorials