Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

Historically 33 seems like a popular choice, and it tends to work pretty well. No one knows why though. For more details, look here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top