I am implementing my own specialized hashmap which has generic value types, but keys are always of type long. Here and there, I am seeing people suggesting that I should multiply key by a prime and then get modulo by number of buckets:
int bucket = (key * prime) % numOfBuckets;
and I don't understand why? It seems to me that it has exactly the same distribution as simple:
int bucket = key % numOfBuckets;
For example, if numOfBuckets is 8, with second "algorithm" we get buckets like {0, 1, 2, 3, 4, 5, 6, 7} repeating for key = 0 to infinity. In first algorithm for same keys we get buckets {0, 3, 6, 1, 4, 7, 2, 5} (or similar) also repeating. Basically we have the same problem like when using identity hash.
Basically, in both cases we get collisions for keys:
key = x + k*numOfBuckets (for k = 1 to infinity; and x = key % numOfBuckets)
because when we get modulo by numOfBuckets we always get x. So, what's the deal with first algorithm, can someone enlighten me?