Can anyone tell me why the number 5381 is used in the DJB hash function?
The DJB hash function is defined as:
h 0 = 5381
h i = 33h i - 1 + s i
Here's a C implementation:
unsigned int DJBHash(char* str, unsigned int len)
{
unsigned int hash = 5381;
unsigned int i = 0;
for(i = 0; i < len; str++, i++)
{
hash = ((hash << 5) + hash) + (*str);
}
return hash;
}
5381 is just a number that, in testing, resulted in fewer collisions and better avalanching. You'll find "magic constants" in just about every hash algo.