[Math] Probability of getting the sum of two numbers selected from two ranges as odd.

probability

Consider two integers N and M.
One number is selected from a range of 1 to N.
Another number is selected from a range of 1 to M.
What is the probability that the sum of the two numbers is odd?

Note:
I figured out that, whenever either N or M is even, the probability will be 1/2. But what if both or any one of them is odd.

Best Answer

As you correctly pointed out, if either $N$ or $M$ is even, then the probability is $1/2$, by a symmetry argument. Suppose both $M,N$ are odd. Then there are two cases:

  1. We pick exactly $M$ from $[1,M]$
  2. We pick anything else from $[1,M]$.

In case 1, the probability that the sum is odd is $\frac{(N-1)/2}{N}= \frac{N-1}{2N}$, slightly less than half, because slightly less than half of the numbers in $[1,N]$ are even. In case 2, the probability that the sum is odd is 1/2 again, because this is equivalent to picking from $[1,M-1]$. Hence the combined answer is

$$\frac{1}{M}\frac{N-1}{2N} + \frac{M-1}{M}\frac{1}{2}=\frac{MN-1}{2MN}$$

A slightly more compact way to write the answer, which handles the cases of $M,N$ even as well, is $$\left \lfloor \frac{MN}{2}\right\rfloor \frac{1}{MN}$$ where $\lfloor\cdot\rfloor$ denotes the floor function.