0

How does a constant before the key in the formula:

h(k) = (const * key) % m,

affect the distribution of the hash values in the table?

Are there any rules on how to choose such a constant to minimize collisions and get an even distribution of the keys in the hash table?

Kevin
  • 53,822
  • 15
  • 101
  • 132
Merni
  • 2,842
  • 5
  • 36
  • 42
  • It depends heavily on distribution of `key` values. If `key` is distributed uniformly, there can hardly be a reason for having any `const` other than 1. Regarding the rules, the first thing is to avoid divisors of the modulo. E.g. if `m` is divisible by `const`, some of hashtable buckets will never get hit. – Alexey Biryukov Oct 08 '13 at 20:41

1 Answers1

1

The constant factor should be prime, and if I remember correctly it should be relatively prime w.r.t. the modulus. This is all discussed at great length in Knuth Volume III.

user207421
  • 305,947
  • 44
  • 307
  • 483