# Generating Randomly distributed points inside a given triangle

Given the cartesian coordinates of three vertices of a triangle $P_1$, $P_2$, $P_3$ I know (have simulated) that I get randomly distributed points by using this protocol:

$s=\text{rand}(0,1)\quad t=\sqrt{\text{rand}(0,1)}$

$P = (1-t)\cdot P_1 + t\cdot\left( (1-s)\cdot P_2 + s\cdot P_3\right )$

where $\text{rand}()$ is the uniform random number generator.

Can someone explain why this works?

Intuitively, this seems like applying the lever rule on the edge between one pair of points, say, $P_2$ & $P_3$ to get a intermediate point $P'$ & then a second lever rule between $P'$ and $P_1$.

What I don't get is why the second lever is drawn from a square root of the first uniform distribution.

$$Q_s=(1-s) P_2 + s P_3$$ is a convex combination of $P_2$ and $P_3$ and $$P_{s,t} = (1-t) P_1 + t Q_s$$ is a convex combination of $P_1$ and $Q_s$, hence a point of the convex envelope of $P_1,P_2,P_3$, i.e. the original triangle. If we draw a random point $P$ in the triangle from a uniform distribution, $P=P_{s,t}$ for some $(s,t)\in[0,1]^2$, and the marginal distributions of $s$ and $t$ can be recovered in a straightforward way. For any $\lambda\in[0,1]$, both $$\mathbb{P}[s\leq \lambda]\qquad \mathbb{P}[t\leq \lambda]$$ can be computed in terms of triangle areas, but while $\mathbb{P}[s\leq \lambda]=\lambda$, $\mathbb{P}[t\leq \lambda]=\lambda^\color{red}{2}$.
So $s$ has a uniform distribution, but the distribution of $t$ is the square root of a uniform distribution.
• @curious_cat: Draw the entire triangle and the one for $t\le\lambda$ -- you'll see that the latter is the former scaled by $\lambda$ in both dimensions. – joriki May 16 '16 at 10:23
• @curious_cat I meant to say: sure you do not see it there.To keep uniformity, you have to combine the three components linearly (Tri-linearly). Your combination involves instead the product $t s$ of the parameters, since you made a double combination. Then Jack already answered about. – G Cab May 14 '16 at 16:51