Array range and shuffle - Node.js Array

Node.js examples for Array:Shuffle

Description

Array range and shuffle

Demo Code

Array.range = function(start, end) {
    if (!end) {/*from  w  ww  .  j a  v a  2 s . c o  m*/
        end = start;
        start = 0;
    }

    var arr = [];
    for (var i = start; i < end; i++)
        arr.push(i);
    return arr;
};

Array.prototype.shuffle = function(seed) {
    var random = new Random(seed);
    var currentIndex = this.length, temporaryValue, randomIndex;

    while (0 !== currentIndex) {
        randomIndex = Math.floor(random.nextFloat() * currentIndex);
        currentIndex -= 1;

        temporaryValue = this[currentIndex];
        this[currentIndex] = this[randomIndex];
        this[randomIndex] = temporaryValue;
    }

    return this;
};

Array.prototype.extend = function(array) {
    this.push.apply(this, array);
};

Related Tutorials