Before entering, the mathematicians agree on a choice of representatives for real sequences when two sequence are equivalent if they are equal past some index ; and a re-labeling of $\Bbb N$ into $M \times \Bbb N$ where $M$ is the set of mathematicians.
Once a mathematician $m$ is in the room, he opens every box not labeled $(m,x)$ for $x \in \Bbb N$, and for $m' \neq m$ he carefully notes the greatest index $x(m')$ (which is independent of $m$) where the sequence $(m',x)$ has a different value from that of its corresponding representative, and $x(m') = -1$ if it is the representative.
Then, $m$ computes $y(m) = \max_{m' \neq m} x(m') +1$, and opens every box labeled $(m,x)$ for $x > y(m)$. He finds the representative of that sequence, and guesses what's inside box $(m,y(m))$ according to that representative. He has the risk of guessing wrong if $y(m) \le x(m)$ (he is the only one not knowing the value of $x(m)$).
If there is an $m$ such that $x(m') < x(m)$ for every $m' \neq m$, then $m$ will be the only mathematician that can answer wrongly (for the others, $y(m') > x(m) > x(m')$). If there are several $m$ whose $x(m)$ tie for greatest, then they will all answer correctly.
The original problem: Two numbers $X,Y$ are chosen according to some distribution on $\mathbb{R}$ and put into envelopes. You are allowed to open one envelope and see $X$, and then decide whether to keep $X$ or discard it and take $Y$, hidden in the other envelope. Your goal is to choose the larger number.
A strategy that gives you probability $\frac{1}{2} + \varepsilon$ of choosing the larger number is to come up with your own distribution whose support is all of $\mathbb{R}$ and sample it to get $Z$. If $Z > X$, discard $X$; otherwise, keep $X$. If $Z$ is less than both $X$ and $Y$, or if $Z$ is greater than both $X$ and $Y$, you will choose the correct number with probability $\frac{1}{2}$. If $X < Z < Y$ or $Y < Z < X$, you will always choose the correct number. The probability of $Z$ being between $X$ and $Y$ is $\varepsilon > 0$, so you will win this game with probability $\frac{1}{2} + \varepsilon$.
The strategy above relies on the fact that no matter how $X,Y$ are distributed, the probability that $Z$ lies between them is greater than $0$. If we are restricted to $[A,B]$, then choosing $Z$ uniformly on $[A,B]$ ensures this unless $X,Y$ are always equal to some fixed constant (in which case the problem is degenerate anyway — it is implied that $P(X = Y) = 0$). The original strategy translates to the new situation without any problems.
Note that we don't actually do very well in either the original situation or the revised one: an adversary can just choose $X,Y$ distributed uniformly on $[\alpha,\alpha+\delta]$ for some $\alpha$ and a tiny $\delta$ and force our advantage $\varepsilon$ to be as small as he wants.
Best Answer
As it has been pointed out, the wikipedia page contains more than enough information. Here my answer anyway:
Let $X$ be a r.v. with a distribution of your choice. The only important thing is that it gives positive weight to each (nonempty) interval of the reals. For example, let $X$ be a standard normal variable.
Let the two numbers be $a$ and $b$, with $a < b$. Compare a realisation of $X$ (independent of your choice of $a$ or $b$) with the value of the first number you see. If $X$ is bigger, switch, otherwise keep the number.
The probability of winning can be computed as follows: