문제

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).

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top