How likely is it to get a HashCode collision with the function below in following scenarios.
- With random int values for key[0],key[1], key[2], key[3]
- With random key values with the following constraints
- key[0] <1,000,000
- key[1] <10,000
- key[2] <1,000
- key[3] <1,000
Assume we have 10 Million objects.
int[] key=new int[4];
public override int GetHashCode()
{
// Use large prime multiples to create a unique hash key
// Create the hash offsets using a "even powers of 2 minus 1" method, which gives
// primes most of the time.
int hashKey = 0;
hashKey += 2047 * key[0];
hashKey += 8191 * key[1];
hashKey += 32767 * key[2];
hashKey += 131071 * key[3];
return hashKey;
}
I wrote a quick script to test this.
When I ran it, it told me I got 23735 collisions.
I also tried it on one million elements, and I got 247 collisions. Both numbers are averages over 4 runs.