gpt4 book ai didi

algorithm - 存储桶实例的哈希键

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:00:39 25 4
gpt4 key购买 nike

在映射 hash key --> bucket instance 时,哪种算法产生最佳分布?

换句话说,假设我有一个哈希函数(可能是 SHA-1)并且我有 n 个桶;我使用什么算法将 key 映射到存储桶?例如。低位、高位,还是其他?

最佳答案

通常,您只需使用桶的数量修改您的哈希值。万一桶的数量是 2 的幂,您可以使用按位与代替。

摘自维基百科 hash function :

A common solution is to compute a fixed hash function with a very large range (say, 0 to 232 − 1), divide the result by n, and use the division's remainder. If n is itself a power of 2, this can be done by bit masking and bit shifting. When this approach is used, the hash function must be chosen so that the result has fairly uniform distribution between 0 and n−1, for any n that may occur in the application. Depending on the function, the remainder may be uniform only for certain n, e.g. odd or prime numbers.

关于algorithm - 存储桶实例的哈希键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7996580/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com