Question

I made a simple hash function (if it can be called one) that converts a string into a double.

It works by taking the value of the first character and casting it to double, then multiplying it with the cosine of the next character, then multiplied with the cosine of the next character and so on...

this is the function:

double hash (string str) {
    double hash = (double)str[0];

    for (int i = 1; i < str.length(); i++) {
        hash *= cos((double)str[i]);
    }

    return hash;
}

So how do I compute for the probability of collisions in this function?

I found one formula which goes 1 - e^(k(k-1)/(2k)), but from what I read it only works if the hash function is a good function (it distributes hash values evenly,like a good RNG, or something like that).

Was it helpful?

Solution

Using floating point math to compute a hash of a string seems a bit overkill. At least one problem with your formula is that permutations of the same string will cause collisions, since multiplication is commutative.

In your case hash('abc') = (cos('a') * cos('b')) * cos('c'), which is equal to hash('cab') = (cos('c') * cos('a')) * cos('b'), except possibly for some minor floating point errors.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top