Nodejs Array Shuffle shuffle()

Here you can find the source of shuffle()

Method Source Code

// shuffles (randomizes an array)
Array.prototype.shuffle = function() {
  var currentIndex = this.length, temporaryValue, randomIndex;
  while (0 !== currentIndex) {
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;//from www  . j  ava  2s  . c o  m
    temporaryValue = this[currentIndex];
    this[currentIndex] = this[randomIndex];
    this[randomIndex] = temporaryValue;
  }
  return this;
}

Related

  1. shuffle()
    Array.prototype.shuffle = function() {
        var i = this.length;
          while(i){
              var j = Math.floor(Math.random()*i);
              var t = this[--i];
              this[i] = this[j];
              this[j] = t;
          return this;
    ...
    
  2. shuffle()
    Array.prototype.shuffle = function() {
      var index = this.length, jIndex, temp;
      if (index == 0) return this;
      while ( --index ) {
        jIndex = Math.floor( Math.random() * ( index + 1 ) );
        temp = this[index];
        this[index] = this[jIndex];
        this[jIndex] = temp;
      return this;
    };
    
  3. shuffle()
    Array.prototype.shuffle = function() {
        for (var i = 0; i < this.length; i++) {
            var j = Math.floor(Math.random() * (this.length - i) + i);
            var temp = this[i];
            this[i] = this[j];
            this[j] = temp;
    };
    String.prototype.capitalise = function() {
    ...
    
  4. shuffle()
    Array.prototype.shuffle = function() {
      this.push(this.shift());
      return this;
    
  5. shuffle()
    Array.prototype.shuffle = function () {
      for (var rnd, tmp, i = this.length; i; rnd = parseInt(Math.random() * i),
          tmp = this[--i], this[i] = this[rnd], this[rnd] = tmp);
    };
    
  6. shuffle()
    Array.prototype.shuffle = function() {
       var i = this.length;
         while (--i) {
            var j = Math.floor(Math.random() * (i + 1));
            var temp = this[i];
            this[i] = this[j];
            this[j] = temp;
       return this;
    ...
    
  7. shuffle()
    Array.prototype.shuffle=function() {
      var i=this.length,j,t;
      while(i--) {
        j=Math.floor((i+1)*Math.random());
        t=arr[i];
        arr[i]=arr[j];
        arr[j]=t;
    
  8. shuffle()
    Array.prototype.shuffle = function () {
      var i = this.length,
        j, t;
      var arr = this;
      while (i--) {
        j = Math.floor((i + 1) * Math.random());
        t = arr[i];
        arr[i] = arr[j];
        arr[j] = t;
    ...
    
  9. shuffle()
    Array.prototype.shuffle = function() {
        for (var j, x, i = this.length - 1; i; j = randomNumber(i), x = this[--i], this[i] = this[j], this[j] = x);
        return this;
    };