views:

502

answers:

4

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.

A: 

My $0.02? Don't use 0.

jacobko
+1  A: 

Historically 33 seems like a popular choice, and it tends to work pretty well. Noone knows why though. For more details, look at http://www.cse.yorku.ca/~oz/hash.html.

wvdschel
+4  A: 

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.

Ryan Fox
+2  A: 

I had an interesting discussion with a coworker about hash function recently. Our conclusions were as follows:

If you really need to write a good hash function that minimizes collisions more than the default implementations available in the standard languages you need an advanced degree in mathematics.

If you're writing applications where a custom hash function will noticeably improve the performance of your application, you're Google and you've got plenty of Math PhDs to do the work.

Sorry to not directly answer your question, but the bottom line is that there's really no need to write your own hash function for String. What language are you working with? I'd imagine there's an easy way to compute a "good enough" hash code.

Mike Deck