Nodejs Array Shuffle shuffle()

Here you can find the source of shuffle()

Method Source Code

/*/*from   w  w w . j a  v  a2s .c o  m*/
    FFDoku is a Sudoku game primarily designed for the Firefox OS
    Copyright (C) 2012 Erwan GUYADER <erwanguyader@mailoo.org>, Antoine DUPARAY <antoineduparay@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as
    published by the Free Software Foundation, either version 3 of the
    License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.


*/

/* To shuffle an array */
Array.prototype.shuffle = function() {
   var s = [];
   while (this.length)
      s.push(this.splice(Math.random() * this.length, 1));
   while (s.length)
      this.push(s.pop());
   return this;
}

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;
      return this;
    
  2. shuffle()
    Array.prototype.shuffle = function (){ 
        for(var j, x, i = this.length; i; j = Math.floor(Math.random() * i), x = this[--i], this[i] = this[j], this[j] = x);
        return this;
    };
    String.prototype.hashCode = function(){
      var hash = 5381;
      for (i = 0; i < this.length; i++) {
        char = this.charCodeAt(i);
        hash = ((hash<<5)+hash) + char;
    ...
    
  3. shuffle()
    Array.prototype.shuffle = function () {
      var i = this.length, j, temp;
      if ( i === 0 ) return false;
      while ( --i ) {
         j = Math.floor( Math.random() * ( i + 1 ) );
         temp = this[i];
         this[i] = this[j]; 
         this[j] = temp;
    
  4. shuffle()
    Array.prototype.shuffle = function() {
      var i = this.length, j, tempi, tempj;
      if ( i == 0 ) return false;
      while ( --i ) {
         j       = Math.floor( Math.random() * ( i + 1 ) );
         tempi   = this[i];
         tempj   = this[j];
         this[i] = tempj;
         this[j] = tempi;
    ...
    
  5. 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;
      return this;
    
  6. shuffle()
    var binarySearch = function(needle, haystack) {
        var low = 0,
            high = haystack.length - 1;
        haystack.sort(function(x, y) {
            return x - y;
        });
        while (low <= high) {
            var middle = Math.floor((low + high) / 2);
            if (needle === haystack[middle])
    ...
    
  7. shuffle()
    Array.prototype.shuffle = function (){
        var i = this.length, j, temp;
        if ( i == 0 ) return;
        while ( --i ) {
            j = Math.floor( Math.random() * ( i + 1 ) );
            temp = this[i];
            this[i] = this[j];
            this[j] = temp;
    };
    
  8. 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;
      return this;
    window.getDeck = function() {
      var ranks = [2, 3, 4, 5, 6, 7, 8, 9, 10, 'jack', 'queen', 'king', 'ace'];
      var suits = ['clubs', 'diamonds', 'hearts', 'spades'];
      var cards = [];
      ranks.forEach(function(rank, index) {
        suits.forEach(function(suit, index) {
          cards.push(rank + "_of_" + suit);
        });
      });
      return cards;
    
  9. shuffle()
    Array.prototype.shuffle = function () {
        for (var i = this.length - 1; i > 0; i--) {
            var j = Math.floor(Math.random() * (i + 1));
            var tmp = this[i];
            this[i] = this[j];
            this[j] = tmp;
        return this;
    };
    ...