# uniformly sample a point in a triangle $(1,0,0), (0,1,0), (0,0,1)$

To choose a point with uniform distribution in a triangle $$A:(1,0,0), B:(0,1,0), C:(0,0,1)$$, my thought is to project the triangle onto X-Y plane first, and the projected triangle is $$A:(1,0,0), B:(0,1,0), C':(0,0,0)$$. Then sample a point $$(x,y,0)$$ in the projected triangle. Finally transform the point back to the triangle in the 3D space and the coordinate is $$(x,y,1-x-y)$$.

However, I feel like there are some problems with this approach. The areas of these two triangles are different. Yet the points before and after the tranfor have the same X- and Y-coordinates. Is this correct?

• I would think to sample using barycentric coordinates. Commented Apr 21 at 2:10

Your method does work, because under the transformation $$(x,y,0) \mapsto (x,y,1-x-y)$$, areas remain proportional. So in particular, the area of the triangle $$\mathcal T(t) = \{(0,0), (t,0), (0,t)\}$$ for $$0 \le t \le 1$$ is $$t^2/2$$, and under transformation, the triangle is mapped to $$\mathcal T'(t) = \{(0,0,1), (t,0,1-t), (0,t,1-t)\}$$ which has area $$t^2 \frac{\sqrt{3}}{2}$$. So, provided that you select $$(x,y)$$ uniformly in the triangle $$\mathcal T(1)$$, then $$(x,y,1-x-y)$$ will be uniformly chosen in $$\mathcal T'(1)$$.

So the only remaining issue is how to choose such $$(x,y)$$ uniformly. This is quite easy, since the conditional density of $$Y$$ given $$X = x \in [0,1]$$ is $$f_{Y \mid X}(y \mid x) = \frac{1}{1-x} \mathbb 1 (0 \le y \le 1-x).$$ So in order for $$(X,Y)$$ to be uniform on $$\mathcal T(1)$$, we must choose $$X \in [0,1]$$ inversely proportional to the length of the support of $$Y \mid X$$; i.e., $$f_X(x) \propto 1-x,$$ hence $$f_X(x) = 2(1-x), \quad 0 \le x \le 1.$$ This is a $$\operatorname{Beta}(1,2)$$ distribution, so we can write $$X \sim \operatorname{Beta}(1,2) \\ Y \mid X \sim \operatorname{Uniform}(0, 1-X) \\ Z = 1 - X - Y.$$

The choice of $$X$$ is the most difficult part, but we can use the inverse transform sampling method because this particular density has an easily invertible CDF. We choose $$U \sim \operatorname{Uniform}(0,1)$$, then compute $$X = 1 - \sqrt{1 - U}$$, which will be $$\operatorname{Beta}(1,2)$$ distributed. Since $$U$$ is symmetric on $$[0,1]$$, we can eliminate one operation and just let $$X = 1 - \sqrt{U}$$.

An alternative approach is to compute $$(X,Y)$$ uniform on the square $$[0,1]^2$$, and then employ a conditional: if $$X+Y > 1$$, then let the selected point be $$(1-X, 1-Y, X+Y-1)$$, in effect rotating the point $$(X,Y)$$ about $$(1/2, 1/2)$$. So this algorithm looks like this: $$X' \sim \operatorname{Uniform}(0,1) \\ Y' \sim \operatorname{Uniform}(0,1) \\ (X,Y,Z) = \begin{cases}(X',Y',1-X'-Y'), & X' + Y' \le 1 \\ (1-X', 1-Y', X'+Y'-1), & X' + Y' > 1. \end{cases}$$ I'm not actually sure which approach is faster, but the square root in the first method may be more computationally expensive.

• Your $X=1-\sqrt{U}$ look very close to the solution proposed in my answer. Commented Apr 21 at 9:08
• Thank you so much! The proof is very clear Commented Apr 21 at 13:09

A more general answer, valid for any triangle.

A direct way to get a uniform distribution of points in a triangle $$ABC$$ is by using this barycentrical expression :

$$M=(1-\sqrt{r_{1}})A+(\sqrt{r_{1}}r_{2})B+\sqrt{r_{1}}(1-r_{2})C\tag{1}$$

where $$r_1,r_2,r_3$$ are uniformly distributed on $$[0,1]$$.

Otherwise said, as a two-step barycentration :

$$M=(1-\sqrt{r_{1}})A+\sqrt{r_{1}}\color{red}{\left[r_{2}B+(1-r_{2})C\right]}\tag{2}$$

The earliest version I have found of formulas (1)/(2) is in this interesting ACM 2002 article : see its p. 814 with a graphical "heuristic proof" of formula (2) above given p. 815 that I reproduce here :

Formulas (1)/(2) are mentionned and established here.

Numerous other references and links like this one.

Remark :

My recent encounter with this issue is in this question. My answer attests that I wasn't convinced at first look (maybe because they had been found by Chat GPT) : I was totally wrong.

• Thank you so much! Commented Apr 21 at 13:10

You could sample uniformly a point in the square with vertices $$(0,0)$$, $$(1,0)$$, $$(1,1)$$, by taking $$(x,y) \in [0,1]^2$$ uniformly and then map it to $$(\max(x,y), \min(x,y))$$. Once you can sample uniformly in one triangle, it can be done in any triangle.

Similarly one can sample uniformly in an $$n$$-dimensional simplex, for small $$n$$. Not clear how expensive it would be to rearrange ( or test the order) if $$n$$ is say $$10000$$.