Nodejs Array Shuffle shuffleMe()

Here you can find the source of shuffleMe()

Method Source Code

Array.prototype.shuffleMe = function() {
    // Get the length
    var array_length = this.length;

    // during looping it will start with the length of the array
    // which is the highest value of the array
    var loop_no = array_length;
    while (--loop_no > 0) {
        // generate a random number
        var random_number = Math.floor(Math.random() * (loop_no + 1));
        // console.log(random_number);

        // get the value of the array in the random no position.
        var value = this[random_number];

        // now re-assign the array value above with a value
        // from the same array, value provided by loop_no position
        this[random_number] = this[loop_no];

        // now give the value variable to the array position thar replaced it
        this[loop_no] = value;//from   w w w  . ja v a  2 s.  com
    }

    return this;
}

var array = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'];
var result = array.shuffleMe();
console.log(result);

Related

  1. shuffle()
    Array.prototype.shuffle = function(){
      var counter = this.length, temp, index;
      while (counter > 0) {
        index = (Math.random() * counter--) | 0;
        temp = this[counter];
        this[counter] = this[index];
        this[index] = temp;
      return this;
    ...
    
  2. shuffle(b)
    function random(min, max) 
      return Math.floor(Math.random() * (max - min + 1)) + min; 
    };
    function getRandomColor()
      return 'rgb(' + random(0, 255) + ', ' + random(0, 255) + ', ' + random(0, 255) + ')';
    };
    function generateRandomSymbols()
    ...
    
  3. shuffle(maxLength)
    Array.prototype.shuffle = function (maxLength) {
        var currentIndex = this.length
        var temporaryValue;
        var randomIndex;
        var result;
        maxLength = maxLength || this.length
        while (0 !== currentIndex) {
          randomIndex = Math.floor(Math.random() * currentIndex);
          currentIndex -= 1;
    ...
    
  4. shuffle(n)
    Array.prototype.shuffle = function (n) {
        var params = [].slice.call(arguments);
        var index = -1,
            result = this,
            length = result.length,
            lastIndex = length - 1;
        while (++index < n) {
            var rand = index + Math.floor(Math.random() * (lastIndex - index + 1));
                value = result[rand];
    ...
    
  5. shuffle(times)
    Array.prototype.shuffle = function(times) {
        if(undefined === times) times = this.length * 2;
        var temp;
        while(times--) {
            var a = Number.random(0, this.length - 1);
            var b = Number.random(0, this.length - 1);
            temp = this[a];
            this[a] = this[b];
            this[b] = temp;
    ...
    
  6. shuffled()
    Array.prototype.shuffled = function() {
      return this.map(function(n){ return [Math.random(), n] })
                 .sort().map(function(n){ return n[1] });
    };
    
  7. shuffle(myArray)
    function shuffle(myArray) {
      var i = myArray.length;
      if ( i == 0 ) return false;
      while ( --i ) {
         var j = Math.floor( Math.random() * ( i + 1 ) );
         var tempi = myArray[i];
         var tempj = myArray[j];
         myArray[i] = tempj;
         myArray[j] = tempi;
    ...
    
  8. shuffleArray(arr)
    Math.shuffleArray = function(arr)
      var i = arr.length;
      if ( i == 0 ) return false;
      while ( --i ) {
      var j = Math.floor( Math.random() * ( i + 1 ) );
      var tempi = arr[i];
      var tempj = arr[j];
      arr[i] = tempj;
    ...
    
  9. doShuffle()
    Array.prototype.doShuffle = function() {
      var j, x, i;
      for(i = this.length-1; i >= 0; --i) {
        j = Math.floor(Math.random() * i);
        x = this[i]; this[i] = this[j]; this[j] = x;
    };
    Array.prototype.shuffle = function() {
      var r = this.slice();
    ...