Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Choosing a multiplier for a (string) hash function

Do you have any advice/rules on selecting a multiplier to use in a (multiplicative) hash function. The function is computing the hash value of a string.

like image 859
David Avatar asked Aug 19 '08 20:08

David


People also ask

Why does Java's hashCode in string use 31 as a multiplier?

1 Answer. This is what described in Effective Java (a book): The value 31 was chosen because it is an odd prime. If it were even and the multiplication overflowed, information would be lost, as multiplication by 2 is equivalent to shifting.

How do you choose a good hash function?

Choosing a good hashing function, h(k), is essential for hash-table based searching. h should distribute the elements of our collection as uniformly as possible to the "slots" of the hash table. The key criterion is that there should be a minimum number of collisions. will provide uniform hashing.

What is the hash function used in multiplication method?

What is the hash function used in multiplication method? Explanation: The hash function can be computed by multiplying m with the fractional part of kA (kA mod 1) and then computing the floor value of the result.


1 Answers

You want to use something that is relatively prime to the size of your set. That way, when you loop around, you won't end up on the same numbers you just tried.

like image 136
Ryan Fox Avatar answered Sep 21 '22 17:09

Ryan Fox