Uniforms summing to a uniform

A code golf question by xnor led to the following nice problem: let X and Y be 2 random variables such that marginally, X\sim U(0,1) and Y\sim U(0,1). Find a joint distribution of (X,Y) such that X+Y\sim U(\frac12, \frac32).

You need X and Y to be negatively correlated for this. I wrote the problem in the lab coffee room, leading to nice discussions (see also Xian’s blog post). Here are two solutions to the problem:

1. Let X\sim U(0,1) and Y = \begin{cases}1-2X\text{ if }X<\frac12\\2-2X\text{ if } X\geq\frac12\end{cases}.  Then:

  • Y|X<\frac12 and Y|X\geq\frac12 are both U(0,1), hence Y\sim U(0,1)
  • X+Y|X<\frac12 = 1-X|X<\frac12\sim U(\frac12, 1) and X+Y|X\geq\frac12 = 2-X|X\geq\frac12\sim U(1, \frac32) hence X+Y\sim U(\frac12, \frac32)

2. A second solution, found by my colleague Amic Frouvelle, is to sample (X,Y) uniformly from the black area:


I quite like that the first solution is 1d but the second is 2d.