Given four different natural numbers $v1, v2, v3, v4\in [0, n]$ ($n$ is any finite number), what is the probability of $v1 + v2 = v3 + v4$.
I'm trying to know the probability of collision of a hash function of mine and I've reduced my problem to calculate that specific situation.
NOTE: I don't know if the numbers being bounded by an interval matters, but I've added it just in case. If that can change, refine or simplify the probability calculation, $n$ is actually $2^x$ where $x$ is probably 64.
NOTE: If applying some efficient bit-wise operation (like $XOR$, or $AND$), or multiplying, instead of summing, could reduce drastically the probability of collision, a comment will be appreciated.
The number of all the quadruples in $[0,n]$ is clearly $$ \left( {n + 1} \right)^{\,4} $$
The number of couples satisfyng $$ \left\{ \matrix{ 0 \le v_{\,1} ,v_{\,2} \le n \hfill \cr v_{\,1} + v_{\,2} = s \hfill \cr} \right. $$ is $$ \left\{ {\matrix{ {s + 1} & {\left| {\,0 \le s \le n} \right.} \cr {2n - s + 1} & {\left| {\,n < s \le 2n} \right.} \cr } } \right. $$ as can be verified geometrically, drawing the line $v_{\,1} + v_{\,2} = s$ inside the square $[0,n]^2$.
Same for $v_{\,3} + v_{\,4} = s$.
Therefore the number of ways to have $v_{\,1} + v_{\,2} = v_{\,3} + v_{\,4} = s$ will be the square of the above, and the total will be given by the sum over $s$, i.e. $$ \eqalign{ & 2\sum\limits_{s = 0}^{n - 1} {\left( {s + 1} \right)^{\,2} } + \left( {n + 1} \right)^{\,2} = {{n\left( {2n + 1} \right)\left( {n + 1} \right)} \over 3} + \left( {n + 1} \right)^{\,2} = \cr & = {{\left( {n + 1} \right)\left( {2n^{\,2} + 4n + 3} \right)} \over 3} \cr} $$
Thus the probability you are looking for is $$ p(n) = {{2n^{\,2} + 4n + 3} \over {3\left( {n + 1} \right)^{\,3} }} $$