In the following situations, how likely is it to get a hashcode collision with the function below.
- with random key values with the following constraints
- Key [0] <1, 000
- Key [1] & lt; 10,000
- The key [2] & lt; 1,000
- The key [3] & lt; 1,000
Let's say we have 10 million objects.
int [] key = new int [4]; Public override int GetHashCode () {// Use large key properties to create a unique hash key // Create a hash offset using the "2-zero 1 power too" method, which is the most time / int hash = 0; Hash + = 2047 * key [0]; Hash + = 8191 * key [1]; Hash + = 32767 * key [2]; Hashke + = 131071 * key [3]; The return hash; }
I have written a quick script to test it.
hash = 0 hash + = 2047 * key is [0] hash + = 8191 * key is [1] hash = + 32767 * key [2] hash + = 131071 * key [3] returns hash View = set () in collision = 0 in the (0, 10000000) category: x = hash ([random.randint (0,1000000), random.randint (0,10000), random.randint (0,1000), Random.randint (0,1000)]) If seen in X: collisions + = 1 else: seen.add (x) print collisions
When I ran it, it told me 23735 Conflicts I also tried one million elements, and I got 247 conflicts. Both numbers are more than 4 runs average.
Comments
Post a Comment