1 of 3

JavaScript 4line shuffle

// globals
var deck = [];

// functions
function newDeck() {
  var a = [], i=52;
  while(i--)a[i]={value:i};
  return a;
}

// random range
function rand(min, max) {
  return Math.floor(Math.random()*(max-min+1)) + min
}

// xor swap by reference
function swap(x, y) {  
  x.value ^= y.value;
  y.value ^= x.value;
  x.value ^= y.value;
}

// Convert deck to string.
function deckStr() {
  var out ='';
  for (var i = 0; i < deck.length; i++) {
    out += deck[i].value + '\n';
  }
  return out;
}

// shuffle without locals
function shuffle() {
  while(deck[0].value!==51)swap(deck[0],deck[rand(1,51)]);
  while(--deck[0].value !== 0)swap(deck[deck[0].value],deck[rand(2,51)])
  deck[0].value=51
  if (rand(0,51) !== 51) swap(deck[0],deck[rand(1,51)]);
}

// Main
(function main() {
  deck = newDeck();
  shuffle();
  console.log(deckStr());
})();