/* This is not a tool to check goodness of hash function(collision wise), but rather hash dispersion through a hilbert mapping. Ideally we would want to see something which look like noise. The more the noise looking, the random hash dispersion is. There is something interesting to watch some patterns emerge. */ var hash = function(s) { /* Simple hash function. */ var a = 1, c = 0, h, o; if (s) { a = 0; /*jshint plusplus:false bitwise:false*/ for (h = s.length - 1; h >= 0; h--) { o = s.charCodeAt(h); a = (a<<6&268435455) + o + (o<<14); c = a & 266338304; a = c!==0?a^c>>21:a; } } return a; }; javaHashCode = function(str){ var hash = 0; if (str.length == 0) return hash; for (i = 0; i < str.length; i++) { char = str.charCodeAt(i); hash = ((hash<<5)-hash)+char; hash = hash & hash; // Convert to 32bit integer } return hash; } function primes(i) { return (5839 * i) ^ 1080727 } ; function getRandomInt(min, max) { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min + 1)) + min; } //Check this in both Chrome and Firefox. Try also the alternative to see how the distribution changes. function randomInt() { return getRandomInt(Number.MIN_SAFE_INTEGER + 1,Number.MAX_SAFE_INTEGER - 1) // return getRandomInt(Number.MIN_SAFE_INTEGER,Number.MAX_SAFE_INTEGER) } djb2Code = function(str){ var hash = 5381; for (i = 0; i < str.length; i++) { char = str.charCodeAt(i); hash = ((hash << 5) + hash) + char; /* hash * 33 + c */ } return hash; } sdbmCode = function(str){ var hash = 0; for (i = 0; i < str.length; i++) { char = str.charCodeAt(i); hash = char + (hash << 6) + (hash << 16) - hash; } return hash; } loseCode = function(str){ var hash = 0; for (i = 0; i < str.length; i++) { char = str.charCodeAt(i); hash += char; } return hash; } stringHash = function(str) { var hash = 5381, i = str.length while(i) hash = (hash * 33) ^ str.charCodeAt(--i) return hash >>> 0; } let maxPixels = 512*512 //262144 max points to be black let fillPercent = 0.40 //try with lower values something like 0.50,0.60, and try with 1.00 to see what happens. let howMany = maxPixels * fillPercent for (let i = 0; i < howMany; i++) { // Curves.drawHilbertPoint(i) //all pixels should be black. // Curves.drawHilbertPoint(hash(String(i))) // Curves.drawHilbertPoint(randomInt()) // Curves.drawHilbertPoint(primes(i)) // Curves.drawHilbertPoint(javaHashCode(String(i))) // Curves.drawHilbertPoint(djb2Code(String(i))) // Curves.drawHilbertPoint(sdbmCode(String(i))) // Curves.drawHilbertPoint(loseCode(String(i))) // Curves.drawHilbertPoint(stringHash(String(i))) } /* There is a variant function which will first check if the pixel we are about to draw is already black and if it is it will make it white i.e a "collision" It is important to note that this is not actual hash collision it is hilbert collision because there is so many pixels we can draw. If the is another collision it will change the white pixel back to black - it will detect a "collision" only once. */
Eval(execute)
Clear
/* ----COPY----- This is not a tool to check goodness of hash function(collision wise), but rather hash dispersion through a hilbert mapping. Ideally we would want to see something which look like noise. The more the noise looking, the random hash dispersion is. There is something interesting to watch some patterns emerge. */ var hash = function(s) { /* Simple hash function. */ var a = 1, c = 0, h, o; if (s) { a = 0; /*jshint plusplus:false bitwise:false*/ for (h = s.length - 1; h >= 0; h--) { o = s.charCodeAt(h); a = (a<<6&268435455) + o + (o<<14); c = a & 266338304; a = c!==0?a^c>>21:a; } } return a; }; javaHashCode = function(str){ var hash = 0; if (str.length == 0) return hash; for (i = 0; i < str.length; i++) { char = str.charCodeAt(i); hash = ((hash<<5)-hash)+char; hash = hash & hash; // Convert to 32bit integer } return hash; } function primes(i) { return (5839 * i) ^ 1080727 } ; function getRandomInt(min, max) { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min + 1)) + min; } //Check this in both Chrome and Firefox. Try also the alternative to see how the distribution changes. function randomInt() { return getRandomInt(Number.MIN_SAFE_INTEGER + 1,Number.MAX_SAFE_INTEGER - 1) // return getRandomInt(Number.MIN_SAFE_INTEGER,Number.MAX_SAFE_INTEGER) } djb2Code = function(str){ var hash = 5381; for (i = 0; i < str.length; i++) { char = str.charCodeAt(i); hash = ((hash << 5) + hash) + char; /* hash * 33 + c */ } return hash; } sdbmCode = function(str){ var hash = 0; for (i = 0; i < str.length; i++) { char = str.charCodeAt(i); hash = char + (hash << 6) + (hash << 16) - hash; } return hash; } loseCode = function(str){ var hash = 0; for (i = 0; i < str.length; i++) { char = str.charCodeAt(i); hash += char; } return hash; } stringHash = function(str) { var hash = 5381, i = str.length while(i) hash = (hash * 33) ^ str.charCodeAt(--i) return hash >>> 0; } let maxPixels = 512*512 //262144 max points to be black let fillPercent = 1.00 //try with lower values something like 0.50,0.40 let howMany = maxPixels * fillPercent for (let i = 0; i < howMany; i++) { // Curves.drawHilbertPoint(i) //all pixels should be black. // Curves.drawHilbertPoint(hash(String(i))) // Curves.drawHilbertPoint(randomInt()) // Curves.drawHilbertPoint(primes(i)) // Curves.drawHilbertPoint(javaHashCode(String(i))) // Curves.drawHilbertPoint(djb2Code(String(i))) // Curves.drawHilbertPoint(sdbmCode(String(i))) // Curves.drawHilbertPoint(loseCode(String(i))) // Curves.drawHilbertPoint(stringHash(String(i))) } /* There is a variant function which will first check if the pixel we are about to draw is already black and if it is it will make it white i.e a "collision" It is important to note that this is not actual hash collision it is hilbert collision because there is so many pixels we can draw. If the is another collision it will change the white pixel back to black - it will detect a "collision" only once. */