Hash function

There is a pair of hash elements a and b.
We can use the following formula to create a hash function. The
Insert picture description here
magnetic link has a total of 16 bits, and the value range of each bit is 0-f. This is the so-called hash code,
that is, the return of the hash function value.

Baidu Encyclopedia: What exactly is a hash code?
Answer: hashCode is an int type value calculated by jdk according to the address or string or number of the object

What is the range of values ​​that can be represented? ? ? ? ? ? ? ? : Each is 2 to the 16th power, a total of 16 bits, so 16 2 to the 16th power.
Insert picture description here
The nature of the
hash function : the input domain of the hash function can be a very large range , for example, any string, but the output domain is a fixed range (a certain number of bits), assuming it is S (although S may be very Large, but not comparable to the input field), and has the following properties:

  1. A typical hash function has an infinite input range .
  2. When the same input value is passed to the hash function, the return value is the same.
  3. When passing different input values ​​to the hash function, the return value may be the same or different. Of course, because the output domain is uniformly S, there will be different input values ​​corresponding to an element in S (a condition called hash collision ).
  4. The most important property is that the return values ​​obtained from many different input values ​​will be evenly distributed on S. This is the key to evaluating the pros and cons of a hash function . The more evenly all return values ​​obtained from different input values ​​are distributed on S, the better the hash function , and this uniform distribution has nothing to do with the law of the appearance of the input values. For example, the three input values ​​of "aaa1", "aaa2" and "aaa3" are relatively similar, but the results obtained after an excellent hash function calculation should be very different.
  5. Discreteness of hash function: For example, it will be clear that the input domain is the range of 0-98 and the output domain is 0 1 2

Give me 99 different samples, and calculate different outputs 0 1 2 these three values ​​in turn.
Basically, after the calculation, it can be said that
there are 33 inputs distributed in position 0
, 33 inputs distributed in position 1,
and 33 inputs Distributed in 2 locations

The first three properties are the basis of the hash function, and the fourth point is the key to evaluating the pros and cons of a hash function . The more evenly all return values ​​obtained from different input values ​​are distributed on S, the better the hash function , and this This uniform distribution has nothing to do with the law of the appearance of the input value.

For example, the three input values ​​of "aaa1", "aaa2" and "aaa3" are relatively similar, but the results obtained after an excellent hash function calculation should be very different.

For example, in a room. Smashing a bottle of perfume, the molecules of these perfumes will remain immobile. The hash function is the process of completely disrupting it.

We determine the trajectory of each perfume molecule through a hash function. After the hash function is calculated, the perfume molecules will be evenly distributed throughout the room!
Insert picture description here

The hash function is not a random function, it does not have any random components, once the sample is fixed, the return value must be determined!

The hash function will disrupt the input rules.

The return value calculated by the hash function has nothing to do with your original input rule!
Insert picture description here
Insert picture description here
And evenly distributed on 0-1.
For example,
Insert picture description here
if it is evenly distributed on 0-98, then it is also evenly distributed on 0-2

The interviewer will not let us implement the hash function, because there are many hash function algorithms

A hexadecimal character is equal to 4 binary bytes. 16=2^4.
Assuming that a hash function gets the range of 2 to the 64th power,
so we need 2 to the 8th power multiplied by 2 to the 16th power.
Insert picture description here
We get the result In two batches,
Insert picture description here
Insert picture description here
h3 is independent of h1 and h2.
Insert picture description here
We can make 1000 hash functions at
0 positions to get a uniform distribution of 0-f and other positions to get a uniform distribution of 0-f, which are independent of each other.
So, We can get multiple hash functions by combining various positions
Insert picture description here

The hash function is extremely important, even more important
than the hash table, because the implementation of the hash table must know the nature of the hash function!

Beginner 6 just started

Guess you like

Origin blog.csdn.net/Mr_zhang66/article/details/109374701