Skip to content

Instantly share code, notes, and snippets.

@zb3
Created December 8, 2016 13:24
Show Gist options
  • Save zb3/f202af9996fe9ca4681cce1a3031e8b4 to your computer and use it in GitHub Desktop.
Save zb3/f202af9996fe9ca4681cce1a3031e8b4 to your computer and use it in GitHub Desktop.
Shannon entropy quine generator....
//Entropy quine generator....
//of course it all depends on precision...
function display(entropy) {
return 'Shannon entropy of this text is about ' + entropy; //+', 2X entropy: '+(2*entropy);
}
var precision = 15;
var maxTriesPerIter = 400;
var maxIters = 400;
/////////////////////////////
var bestDiff = Infinity;
var diffNow = 0;
var delta = Math.pow(10, precision - 2);
outer: while (maxIters-->0) {
var iter = 0,
cand = Math.random(),
bestCand = '';
while (iter++ < maxTriesPerIter) {
var real = parseFloat(entropy(display(cand)).toFixed(precision));
if (cand == real) {
console.log('Exact match!!!', cand);
console.log(display(cand));
break outer;
} else {
diffNow = Math.abs(cand - real);
if (diffNow < bestDiff) {
bestDiff = diffNow;
bestCand = cand;
console.log('New best candidate', cand, diffNow);
}
cand = parseFloat((real - (Math.random() - 0.5) / delta).toFixed(precision));
}
}
}
//entropy function taken from zb3.strefa.pl/tools.html
function entropy(str) {
var countMap = [],
c;
var ret = 0;
for (var t = 0; t < str.length; t++) {
c = str.charCodeAt(t);
countMap[c] = (countMap[c] || 0) + 1;
}
var ckeys = Object.keys(countMap),
p;
for (var t = 0; t < ckeys.length; t++) {
p = countMap[ckeys[t]] / str.length;
ret += p * Math.log(p);
}
ret /= Math.log(2);
return -ret;
}
@zb3
Copy link
Author

zb3 commented Dec 8, 2016

Shannon entropy of this text is about 4.399250396597324

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment