Nodejs Array Shuffle shuffleArray(arr)

Here you can find the source of shuffleArray(arr)

Method Source Code

Math.shuffleArray = function(arr)
{
   // Fisher-yates, http://sedition.com/perl/javascript-fy.html
   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;//from   w ww  .  j  a  v  a2  s .com
   arr[j] = tempi;
   }
   return arr;
}

Related

  1. 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];
    ...
    
  2. 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;
    ...
    
  3. shuffleMe()
    Array.prototype.shuffleMe = function() {
        var array_length = this.length;
        var loop_no = array_length;
        while (--loop_no > 0) {
            var random_number = Math.floor(Math.random() * (loop_no + 1));
            var value = this[random_number];
            this[random_number] = this[loop_no];
            this[loop_no] = value;
        return this;
    var array = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'];
    var result = array.shuffleMe();
    console.log(result);
    
  4. shuffled()
    Array.prototype.shuffled = function() {
      return this.map(function(n){ return [Math.random(), n] })
                 .sort().map(function(n){ return n[1] });
    };
    
  5. 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;
    ...
    
  6. 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();
    ...