My hashcode function for string is as follows
hashVal=(127*hashVal+key.charAt(i))%16908799
I am following cs61 b lectures online and I dont quite follow when Prof.Jonathan on what would happen if instead of 1690877 we would use a value that is no relatively prime with 127. I understand the simple case where he uses 127 instead of 16908799 but what if it was a simple multiple of 127 ? How would it “bias” the hashvalue ? How does the bias depend on the common factor “x” ? Can anyone suggest me the reason ?
Use smaller numbers and think it out. Say you’re working in a modulus 10 space (instead of 16908799). Your
hashValcan then only contain the numbers 0-9.If you multiply by 7, for instance, you should see that you can get out all numbers 0-9:
However if you multiply by 6 (which is not relatively prime with 10 because they have the common factor 2), then you will not get all numbers 0-9 out, thus there is bias: