Hash function for a string

后端 未结 5 2072
青春惊慌失措
青春惊慌失措 2020-12-01 06:40

We are currently dealing with hash function in my class. Our instructor asked us to a hash function on the internet to compare to the two we have used in our code.

T

5条回答
  •  难免孤独
    2020-12-01 07:06

    Hash functions for algorithmic use have usually 2 goals, first they have to be fast, second they have to evenly distibute the values across the possible numbers. The hash function also required to give the all same number for the same input value.

    if your values are strings, here are some examples for bad hash functions:

    1. string[0] - the ASCII characters a-Z are way more often then others
    2. string.lengh() - the most probable value is 1

    Good hash functions tries to use every bit of the input while keeping the calculation time minimal. If you only need some hash code, try to multiply the bytes with prime numbers, and sum them.

提交回复
热议问题