Nodejs Array Shuffle shuffle()

Here you can find the source of shuffle()

Method Source Code

/**/*  ww w  .  j  ava 2  s  .c o  m*/
 * Shuffle a given array
 * @param {array} array Array to be shuffled
 * @return {array} Suffled array
 */
Array.prototype.shuffle = function () {
    let array = this, arraylength = array.length, randomIndex, temp;

    while (arraylength) {
        randomIndex = Math.floor(Math.random(arraylength) * arraylength--);

        temp = array[arraylength];
        array[arraylength] = array[randomIndex];
        array[randomIndex] = temp;
    }

    return array;
}

// Test 1
console.log([].shuffle());
// Test 2
console.log([0].shuffle());
// Test 3
console.log([1].shuffle());
// Test 4
console.log([1,2].shuffle());
// Test 5
console.log([1,2,3].shuffle());
// Test 6
console.log([1,2,3,4,5,6,7,8,9,10,-1].shuffle());

Related

  1. shuffle()
    Array.prototype.shuffle = function() {
      var currentIndex = this.length, temporaryValue, randomIndex;
      while (0 !== currentIndex) {
        randomIndex = Math.floor(Math.random() * currentIndex);
        currentIndex -= 1;
        temporaryValue = this[currentIndex];
        this[currentIndex] = this[randomIndex];
        this[randomIndex] = temporaryValue;
    
  2. shuffle()
    Array.prototype.shuffle = function() {
        if (Object.prototype.toString.call(this) !== '[object Array]') {
            throw new TypeError("`this` must be Array, not " + typeof this);
        var length = this.length;
        for (var i = length - 1; i >= 0; i--) {
            var randomIndex = Math.floor(Math.random() * length),
                temp = this[i];
            this[i] = this[randomIndex];
    ...
    
  3. shuffle()
    Array.prototype.shuffle = function(){
      var length = this.length;
      var i, j, temp;
      for(i = length - 1; i >= 0; i--){
        j = Math.floor(Math.random()*(i+1));
        temp = this[i];
        this[i] = this[j];
        this[j] = temp;
      return this;
    };
    var testArray = [1,2,3,4,5,6,7,8,9,10];
    console.log(testArray.shuffle());
    console.log(testArray);
    
  4. shuffle()
    Array.prototype.shuffle = function(){
      var result = [];
      while(this.length > 0 ){
        result.push( this.splice(Math.floor(Math.random() * this.length), 1)[0] );
      return result;
    };
    
  5. shuffle()
    Array.prototype.shuffle = function(){
      for (var i = 0; i < this.length; i++){
          var a = this[i];
          var b = Math.floor(Math.random() * this.length);
          this[i] = this[b];
          this[b] = a;
    function shuffleProperties(obj) {
    ...
    
  6. shuffle()
    'use strict';
    Array.prototype.shuffle = function() {
      var arr = this.slice(0);
      var counter = this.length,
        temp, index;
      while (counter > 0) {
        index = Math.floor(Math.random() * counter);
        counter--;
        temp = arr[counter];
    ...
    
  7. shuffle()
    Array.prototype.shuffle=function() {
      this.sort(function() {return Math.random()-0.5; });
    
  8. shuffle()
    Array.prototype.shuffle = function() {
    var s = [];
    while (this.length) s.push(this.splice(Math.random() * this.length, 1)[0]);
    while (s.length) this.push(s.pop());
    return this;
    
  9. shuffle()
    Array.prototype.shuffle = function(){
      var i=this.length,j,t;
      while(i--)
        j=Math.floor((i+1)*Math.random());
        t=this[i];
        this[i]=this[j];
        this[j]=t;
      return this;
    };