哈希函数给了我非常大的数字

我正在使用c的djb2哈希函数,当我通过它运行一个名称时,我得到数十万的哈希值,我希望能够使用几千个数组将其放入哈希表中或者至少在一个长的内部。 我很困惑如何让函数给我较小的哈希值,同时仍然具有哈希的完整性。 另外,我对如何确定用于哈希表的数组的正确大小感到困惑。 先感谢您。

unsigned long hash(char* str) { unsigned long hash = 5381; int c; for (int i = 0; i < strlen(str); ++i) { c = (int) str[i]; hash = ((hash << 5) + hash) + c; } return hash; } 

假设你的djb2版本返回一个unsigned long (比如说调用返回变量foo ),使用表达式取模结果的模数

foo % n

将结果从0限制为包括n - 1 。 这应该具有与原始散列值类似的理想统计特性,并且应该优于通过整数除法获得的结果。