Created
December 8, 2016 13:24
-
-
Save zb3/f202af9996fe9ca4681cce1a3031e8b4 to your computer and use it in GitHub Desktop.
Shannon entropy quine generator....
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Shannon entropy of this text is about 4.399250396597324