Skip to main content
Swap with better randomization.
Source Link
wolfhammer
  • 1.2k
  • 9
  • 7

JavaScript 1 line shuffle4 lines

// globals
var deck = [];

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

// random range
function swap(deck[0],rand(min1, max51) {)
  return Math.floor(Math.random()*while(max-min+1)deck[0]++!=104) + min
}

// xor swap by reference
function swap(xdeck[0]%51+1, y) {
  if rand(x === y1,51) return;
  x.value ^= y.value;
  y.value ^= x.value;
  x.value ^= y.value;
}

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

Original answer that wasn't random enough. The swap wasn't guaranteed to touch each item in the deck.

// shuffle without locals
function shuffle() {
  deck.map(function(){swap(deck[rand(0,51)],deck[rand(0,51)])});
}

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

JavaScript 1 line 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) {
  if (x === y) return;
  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() {
  deck.map(function(){swap(deck[rand(0,51)],deck[rand(0,51)])});
}

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

JavaScript 4 lines

function shuffle() {
  while(deck[0]!=0)swap(deck[0],rand(1,51))
  while(deck[0]++!=104)swap(deck[0]%51+1,rand(1,51))
  deck[0]=0
  swap(0,rand(0,51))
}

Original answer that wasn't random enough. The swap wasn't guaranteed to touch each item in the deck.

// shuffle without locals
function shuffle() {
  deck.map(function(){swap(deck[rand(0,51)],deck[rand(0,51)])});
}
shortened shuffler
Source Link
wolfhammer
  • 1.2k
  • 9
  • 7

JavaScript 4line1 line 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) {
  if (x === y) return;
  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]deck.value!==51)swapmap(deck[0],deck[randfunction(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(10,51)])});
}

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

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());
})();

JavaScript 1 line 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) {
  if (x === y) return;
  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() {
  deck.map(function(){swap(deck[rand(0,51)],deck[rand(0,51)])});
}

// Main
(function main() {
  deck = newDeck();
  shuffle();
  console.log(deckStr());
})();
Source Link
wolfhammer
  • 1.2k
  • 9
  • 7

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());
})();