我知道在异或之前乘以一个大数应该有助于处理分布不均的操作数,但为什么乘数应该是素数?
Related:
Why should hash functions use a prime number modulus?Close, but not quite a Duplicate:
Why does Java’s hashCode() in String use 31 as a multiplier?
最佳答案
有一个 good article on the Computing Life blog详细讨论了这个话题。它最初是作为对我在问题中链接到的 Java hashCode() 问题的回应而发布的。根据文章:
Primes are unique numbers. They are unique in that, the product of a prime with any other number has the best chance of being unique (not as unique as the prime itself of-course) due to the fact that a prime is used to compose it. This property is used in hashing functions.
Given a string “Samuel”, you can generate a unique hash by multiply each of the constituent digits or letters with a prime number and adding them up. This is why primes are used.
However using primes is an old technique. The key here to understand that as long as you can generate a sufficiently unique key you can move to other hashing techniques too. Go here for more on this topic about hashes without primes.
关于java - 在许多 GetHashCode 实现中,为什么在 xoring 之前乘以质数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1488977/