String to Integer Hashing Function with Precision

后端 未结 4 1748
滥情空心
滥情空心 2021-01-19 10:35

I want to hash a char array in to an int or a long. The resulting value has to adhere to a given precision value. The function I\'ve been using is given below:



        
4条回答
  •  南笙
    南笙 (楼主)
    2021-01-19 10:48

    The very definition of a hash is that it produces duplicate values for some values, due to hash value range being smaller than the space of the hashed data.

    In theory, a 32-bit hash has enough range to hash all ~6 character strings (A-Z,a-z,0-9 only), without causing a collision. In practice, hashes are not a perfect permutation of the input. Given a 32-bit hash, you can expect to get hash collisions after hashing ~16 bit of random inputs, due to the birthday paradox.

    Given a static set of data values, it's always possible to construct a hash function designed specifically for them, which will never collide with itself (of course, size of its output will be at least log(|data set|). However, it requires you to know all the possible data values ahead of time. This is called perfect hashing.

    That being said, here are a few alternatives which should get you started (they are designed to minimize collisions)

提交回复
热议问题