哈希函数给了我极大的数字 [英] Hash Function giving me extremely large numbers
问题描述
我正在为c使用djb2哈希函数,当我通过它运行一个名称时,我得到了成千上万的哈希数,我希望能够使用一个数组来将其放入哈希表中几千个或更小的东西,至少在很长的时间内.我对如何获取给我较小的哈希值同时又具有哈希完整性的功能感到困惑.我也很困惑如何决定要用于我的哈希表的数组的正确大小.预先谢谢你.
I am using the djb2 hash function for c, when I run a name through it I am getting hash numbers in the hundreds of thousands, I would like to get to be able to put this in a hash table using an array of a few thousand or something smaller at least inside a long. I am confused about how to get the function to give me smaller hashes while still having the integrity of the hash. Also I am confused about how to decide on the proper size of array to use for my hash table. Thank you in advance.
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
),并以该结果的模为模n
使用表达式
Assuming that your version of djb2
returns an unsigned long
(call the return variable foo
, say), taking the modulus of that result modulo n
using the expression
foo % n
会将结果限制为0
到并包括n - 1
.它应该具有与原始哈希值相似的理想统计属性,并且应该优于通过整数除法获得的结果.
will constrain the result from 0
to and including n - 1
. This ought to have similar desirable statistical properties to the original hash value, and ought to be superior to a result obtained by integer division.
这篇关于哈希函数给了我极大的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!