Let's say there is some positive integer n that is somewhere between 0 and N (also a positive integer). I tell the program to start generating random (or pseudo-random) number pairs (modulo N) and check each time if their difference is n. When n is found, program exits.
pseudo code:
declare n and give it some value;
declare a;
declare b;
--start
a = random() mod N;
b = random() mod N;
if |a-b| = n
{
exit program;
}
else
{
go back to the start line;
}
What is the complexity of such program?
Next, I want to find what is the probability of that n being found after m number of tries (pair difference checks). To make it easier, I reverse the question and ask what is the probability of that n NOT being found after m tries:
(for example, n = 9 and N = 10)
2 = number of pairs that give 9 (I'm checking for absolute values of pair differences)
10^2 = number of all possible pairs
10^2 - 2 = number of pairs that will NOT give 9
So the probability of NOT getting 9 after m tries is: $(\frac{98}{100})^m$
It seems that after around 34 checks I should have about 50% probability of getting 9.
Is my calculation correct? Is this an application of the Birthday problem?
The only way to get $a - b \equiv N \mod N$ when $a$ and $b$ are $0 \le a, b < N$ is that $a = b$. Thus the program stops when the same number is generated twice, which happens once in $N^2$ tries.
The birthday paradox is that the size of the set of values taken independently out of $N$ to get probability $1/2$ of some element repeating is around $\sqrt{N}$, but one would naïvely think it needs to be much larger.