Choosing a multiplier for a (string) hash function

后端 未结 3 2044
小鲜肉
小鲜肉 2021-02-05 14:03

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.

相关标签:
3条回答
  • 2021-02-05 14:29

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

    0 讨论(0)
  • 2021-02-05 14:33

    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.

    0 讨论(0)
  • 2021-02-05 14:47

    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.

    0 讨论(0)
提交回复
热议问题