hash - Why are 5381 and 33 so important in the djb2 algorithm? -
is a hash function for the string
unsigned long hash = 5381; Int c; While (c = * str ++) hash = ((hash> <5) + hash) + c; Why 5381 and 33 are so important?
"text"> this hash function is a ( LCG - is the simple category of function that generates a series of psuedo-random numbers), which is usually the form:
X = (one * x) + c; // "Mod M", where M = 2 ^ 32 or 2 ^ 64 usually
Note the similarity of the DBB2 hash function ... a = 33, M = 2 ^ 32 For a LCG, there should be special properties in "full duration" (i.e. that may be random), one :
- A-1 divisor All the major factors in M's (A1's 32, which is divisible by 2, is the only main factor of 2 ^ 32)
- 4 is a lot bigger if a multi-m4 (yes and Yes)
In addition, C and < Em> M is considered to be relatively prominent (which would be right for the strange value c )
So as you can see, this hash function resembles some good LCG and when it comes to hash functions, you want to have a "random" distribution of a hash value that is real A real set of string is given to.
This is the reason why this hash function is good for strings, I think it is a good balance to be fast, giving proper distribution of hash values but I have seen many other hash functions Claims better output characteristics, but include many more lines of code, see for example
Edit: Tells why 33 and 5381 are for practical reasons. Who were.
Comments
Post a Comment