Skip to content

Instantly share code, notes, and snippets.

@greyaperez
Created February 10, 2016 19:13
Show Gist options
  • Save greyaperez/854fa6c7206447734da6 to your computer and use it in GitHub Desktop.
Save greyaperez/854fa6c7206447734da6 to your computer and use it in GitHub Desktop.
Predictable Random - Is Random Truly Random?
/**
* Predictable Random - Is Random Truly Random?
* @description - Not the cleanest code, but more of a proof of concept quick flesh out.
* @author Timothy A. Perez <tim.adam.perez@gmail.com>
*/
///////////////////////////////////////////////////
var sampleTest = (function a(limit, last, r, i, repeatLog) {
if (!limit) {
console.log('Result: ', repeatLog);
repeatLog = null;
return;
}
var x;
if (i > limit) {
var result = _.clone(repeatLog);
repeatLog = null;
return repeatLog;
}
x = _.sample(['a','b','c']);
if (x === last) {
r++;
} else {
if (r > 1) {
repeatLog[last][r] = (!_.has(repeatLog[last], r)) ? 1 : repeatLog[last][r] + 1;
}
r = 1; // Restart Repeat Counter
last = x;
}
a(--limit, last, r, i, repeatLog);
});
var runTest = function runtTest (sampleSize) {
var repeatLog = { 'a':{}, 'b':{}, 'c': {} };
// Quick and dirty load of LoDash if unavailable
if (typeof _ === 'undefined') {
var script = document.createElement('script');
script.type = 'text/javascript';
script.async = true;
script.src = 'https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.js';
script.onload = function(){
sampleTest(sampleSize, 'a', 0, 1, repeatLog);
};
document.getElementsByTagName('head')[0].appendChild(script);
} else {
sampleTest(sampleSize, 'a', 0, 1, repeatLog);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment