What integer hash function are good that accepts an integer hash key?
Depends on how your data is distributed. For a simple counter, the simplest function
f(i) = i
will be good (I suspect optimal, but I can't prove it).
This page lists some simple hash functions that tend to decently in general, but any simple hash has pathological cases where it doesn't work well.
The answer depends on a lot of things like:
- Where do you intend to employ it?
- What are you trying to do with the hash?
- Do you need a crytographically secure hash function?
I suggest that you take a look at the Merkle-Damgard family of hash functions like SHA-1 etc
Thanks mmeyers! For some reason, since the past few days, I:
- can't read post-comments
- can't use hyperlinks properly
- am forced to use CAPTCHA for every single post
There's a nice overview over some hash algorithms at Eternally Confuzzled. I'd recommend Bob Jenkins' one-at-a-time hash which quickly reaches avalanche and therefore can be used for efficient hash table lookup.
Knuth's multiplicative method:
hash(i)=i*2654435761 mod 2^32
In general, you should pick a multiplier that is in the order of your hash size (2^32
in the example) and has no common factors with it. This way the hash function covers all your hash space uniformly.
Edit: The biggest disadvantage of this hash function is that it preserves divisibility, so if your integers are all divisible by 2 or by 4 (which is not uncommon), their hashes will be too. This is a problem in hash tables - you can end up with only 1/2 or 1/4 of the buckets being used.
32-bits multiplicative method (very fast) see @rafal
#define hash32(x) ((x)*2654435761) #define H_BITS 24 // Hashtable size #define H_SHIFT (32-H_BITS) unsigned hashtab[1<<H_BITS] .... unsigned slot = hash32(x) >> H_SHIFT
32-bits and 64-bits (good distribution) at : MurmurHash
- Integer Hash Function