Skip to content

Instantly share code, notes, and snippets.

@radiodario
Created April 29, 2015 18:49
Show Gist options
  • Save radiodario/1ad9a442844926b55be0 to your computer and use it in GitHub Desktop.
Save radiodario/1ad9a442844926b55be0 to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/kimufiwuki
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
// Fisher-Yates Shuffle.
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex ;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
// calculate P(lower|value + seen)
function probabilityLowerCardGiven(value, seen, deck) {
var lowerCardsRemaining = 4 * (value - 1);
// check the ones that have been played
for (var i = 0; i < seen.length; i++) {
if (seen[i] < value) {
lowerCardsRemaining--;
}
}
return (lowerCardsRemaining / deck.length);
}
// calculate P(higher|value + seen)
function probabilityHigherCardGiven(value, seen, deck) {
var higherCardsRemaining = 4 * (13 - value);
// check the ones that have been played
for (var i = 0; i < seen.length; i++) {
if (seen[i] > value) {
higherCardsRemaining--;
}
}
return (higherCardsRemaining / deck.length);
}
function playGame() {
var deck = shuffle([
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, // hearts
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, // clubs
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, // diamonds
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, // spades
]);
var seen = [];
var score = 0;
var card = deck.pop();
var next = deck.pop();
var pLo, pHi;
// go over the whole deck
while (deck.length > 0) {
pLo = probabilityLowerCardGiven(card, seen, deck);
pHi = probabilityHigherCardGiven(card, seen, deck);
// play rationally.
// we go lower
if (pLo > pHi) {
// test if we guessed lower
if (next < card) {
score++;
}
}
// we go higher
else if (pLo < pHi) {
if (next > card) score++;
}
seen.push(card);
card = next;
next = deck.pop();
}
return score;
}
function ComputeAverageScore() {
var numGames = 1000;
var sumScore = 0;
var scores = [];
var thisScore;
for (var i = 0; i < numGames; i++) {
thisScore = playGame();
sumScore += thisScore;
scores.push(thisScore);
}
console.log('average score is ', sumScore/numGames);
return {
avgScore: sumScore/numGames,
scores: scores
};
}
var scores = ComputeAverageScore();
</script>
<script id="jsbin-source-javascript" type="text/javascript">// Fisher-Yates Shuffle.
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex ;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
// calculate P(lower|value + seen)
function probabilityLowerCardGiven(value, seen, deck) {
var lowerCardsRemaining = 4 * (value - 1);
// check the ones that have been played
for (var i = 0; i < seen.length; i++) {
if (seen[i] < value) {
lowerCardsRemaining--;
}
}
return (lowerCardsRemaining / deck.length);
}
// calculate P(higher|value + seen)
function probabilityHigherCardGiven(value, seen, deck) {
var higherCardsRemaining = 4 * (13 - value);
// check the ones that have been played
for (var i = 0; i < seen.length; i++) {
if (seen[i] > value) {
higherCardsRemaining--;
}
}
return (higherCardsRemaining / deck.length);
}
function playGame() {
var deck = shuffle([
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, // hearts
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, // clubs
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, // diamonds
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, // spades
]);
var seen = [];
var score = 0;
var card = deck.pop();
var next = deck.pop();
var pLo, pHi;
// go over the whole deck
while (deck.length > 0) {
pLo = probabilityLowerCardGiven(card, seen, deck);
pHi = probabilityHigherCardGiven(card, seen, deck);
// play rationally.
// we go lower
if (pLo > pHi) {
// test if we guessed lower
if (next < card) {
score++;
}
}
// we go higher
else if (pLo < pHi) {
if (next > card) score++;
}
seen.push(card);
card = next;
next = deck.pop();
}
return score;
}
function ComputeAverageScore() {
var numGames = 1000;
var sumScore = 0;
var scores = [];
var thisScore;
for (var i = 0; i < numGames; i++) {
thisScore = playGame();
sumScore += thisScore;
scores.push(thisScore);
}
console.log('average score is ', sumScore/numGames);
return {
avgScore: sumScore/numGames,
scores: scores
};
}
var scores = ComputeAverageScore();
</script></body>
</html>
// Fisher-Yates Shuffle.
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex ;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
// calculate P(lower|value + seen)
function probabilityLowerCardGiven(value, seen, deck) {
var lowerCardsRemaining = 4 * (value - 1);
// check the ones that have been played
for (var i = 0; i < seen.length; i++) {
if (seen[i] < value) {
lowerCardsRemaining--;
}
}
return (lowerCardsRemaining / deck.length);
}
// calculate P(higher|value + seen)
function probabilityHigherCardGiven(value, seen, deck) {
var higherCardsRemaining = 4 * (13 - value);
// check the ones that have been played
for (var i = 0; i < seen.length; i++) {
if (seen[i] > value) {
higherCardsRemaining--;
}
}
return (higherCardsRemaining / deck.length);
}
function playGame() {
var deck = shuffle([
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, // hearts
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, // clubs
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, // diamonds
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, // spades
]);
var seen = [];
var score = 0;
var card = deck.pop();
var next = deck.pop();
var pLo, pHi;
// go over the whole deck
while (deck.length > 0) {
pLo = probabilityLowerCardGiven(card, seen, deck);
pHi = probabilityHigherCardGiven(card, seen, deck);
// play rationally.
// we go lower
if (pLo > pHi) {
// test if we guessed lower
if (next < card) {
score++;
}
}
// we go higher
else if (pLo < pHi) {
if (next > card) score++;
}
seen.push(card);
card = next;
next = deck.pop();
}
return score;
}
function ComputeAverageScore() {
var numGames = 1000;
var sumScore = 0;
var scores = [];
var thisScore;
for (var i = 0; i < numGames; i++) {
thisScore = playGame();
sumScore += thisScore;
scores.push(thisScore);
}
console.log('average score is ', sumScore/numGames);
return {
avgScore: sumScore/numGames,
scores: scores
};
}
var scores = ComputeAverageScore();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment