I wrote a quick canvas visualization to see the distribution of a hashing algorithm that I ported from C++ to JavaScript.
I'm seeing odd behavior in that no matter what I mod the hash by, 0 is heavily biased, in that it is selected exactly twice as often than most other numbers from the hashing function.
You can see the demo at: http://jsfiddle.net/x5L73/2/
and the original C++ algorithm: http://www.azillionmonkeys.com/qed/hash.html
And the part of the code I'm referring to, which is at the bottom of the jsFiddle:
// hash is 0 twice as often as anything else
var hash = app.Hash( word ) % ( 3499 )
, b1 = 0|hash / 59
, b2 = hash % 59;
What is odd to me is that hash
is zero twice as often as it is any other value, regardless of what I choose to mod it by. In this example, it is zero 1/3499
times, whereas any other number is hit 1/6998
times. This was determined through brute force testing via:
if( hash!==1234 ){ nonZero++; }else{ zero++ } // 1234 is a random number to check
if( Math.random() < .00001 ){ console.log( zero, nonZero, 0|nonZero/zero ); }
What am I missing here???