Hash Function giving me extremely large numbers

元气小坏坏 提交于 2019-12-12 03:43:16

问题


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;
}

回答1:


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

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.



来源:https://stackoverflow.com/questions/39979114/hash-function-giving-me-extremely-large-numbers

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!