/************ CoffeeScript source: ************
deckSize = 52
deck = [0...deckSize]
rand = (min = 0, max = deckSize) ->
Math.floor(Math.random() * (max - min)) + min
swap = (a, b) ->
[deck[a], deck[b]] = [deck[b], deck[a]]
testCount = 10000
log2 = (x) -> Math.log(x) / Math.LN2
test = () ->
$('
').appendTo 'body'
plot do () ->
for [1..testCount]
deck = [0...deckSize]
do shuffle
for i in [0...deckSize]
if deck.indexOf(i) is -1 then throw "#{i} is not in the deck"
if deck.length isnt deckSize then throw "deck length is #{deck.length}; should be #{deckSize}"
deck
plot = (runs) ->
values = ((0 for [0...deckSize]) for [0...deckSize])
for run in runs
for card, index in run
values[card][index]++
for i in [0...deckSize]
tr = $ ''
for v in values[i]
j = v * Math.floor(log2(testCount) / 6) % 256
tr.append """
| """
tr.appendTo 'table'
$ () ->
test deck
*********************************************/
//Usage:
//1. Include jQuery
//2. Declare your shuffle() function globally
var deck, deckSize, log2, plot, rand, swap, test, testCount, _i, _results;
deckSize = 52;
deck = (function() {
_results = [];
for (var _i = 0; 0 <= deckSize ? _i < deckSize : _i > deckSize; 0 <= deckSize ? _i++ : _i--){ _results.push(_i); }
return _results;
}).apply(this);
rand = function(min, max) {
if (min == null) min = 0;
if (max == null) max = deckSize;
return Math.floor(Math.random() * (max - min)) + min;
};
swap = function(a, b) {
var _ref;
return _ref = [deck[b], deck[a]], deck[a] = _ref[0], deck[b] = _ref[1], _ref;
};
testCount = 10000;
log2 = function(x) {
return Math.log(x) / Math.LN2;
};
test = function() {
$('').appendTo('body');
return plot((function() {
var i, _j, _k, _l, _results1, _results2;
_results1 = [];
for (_j = 1; 1 <= testCount ? _j <= testCount : _j >= testCount; 1 <= testCount ? _j++ : _j--) {
deck = (function() {
_results2 = [];
for (var _k = 0; 0 <= deckSize ? _k < deckSize : _k > deckSize; 0 <= deckSize ? _k++ : _k--){ _results2.push(_k); }
return _results2;
}).apply(this);
shuffle();
for (i = _l = 0; 0 <= deckSize ? _l < deckSize : _l > deckSize; i = 0 <= deckSize ? ++_l : --_l) {
if (deck.indexOf(i) === -1) throw "" + i + " is not in the deck";
if (deck.length !== deckSize) {
throw "deck length is " + deck.length + "; should be " + deckSize;
}
}
_results1.push(deck);
}
return _results1;
})());
};
plot = function(runs) {
var card, i, index, j, run, tr, v, values, _j, _k, _l, _len, _len1, _len2, _m, _ref, _results1;
values = (function() {
var _j, _results1;
_results1 = [];
for (_j = 0; 0 <= deckSize ? _j < deckSize : _j > deckSize; 0 <= deckSize ? _j++ : _j--) {
_results1.push((function() {
var _k, _results2;
_results2 = [];
for (_k = 0; 0 <= deckSize ? _k < deckSize : _k > deckSize; 0 <= deckSize ? _k++ : _k--) {
_results2.push(0);
}
return _results2;
})());
}
return _results1;
})();
for (_j = 0, _len = runs.length; _j < _len; _j++) {
run = runs[_j];
for (index = _k = 0, _len1 = run.length; _k < _len1; index = ++_k) {
card = run[index];
values[card][index]++;
}
}
_results1 = [];
for (i = _l = 0; 0 <= deckSize ? _l < deckSize : _l > deckSize; i = 0 <= deckSize ? ++_l : --_l) {
tr = $('');
_ref = values[i];
for (_m = 0, _len2 = _ref.length; _m < _len2; _m++) {
v = _ref[_m];
j = v * Math.floor(log2(testCount) / 6) % 256;
tr.append("\n \n | ");
}
_results1.push(tr.appendTo('table'));
}
return _results1;
};
$(function() {
return test(deck);
});