Random points in a triangle

randomness
triangles
barycentric coordinates
Author

Apurva Nakade

Published

May 12, 2025

Randomness is a surprisingly hard thing to simulate. Any program, by definition, has a predictable output. The best you can hope for is to predictably generate points that look random (pseudo-random) and fool some statistical tests. All random generation relies on two things:

  1. A pseudo-random number generator that outputs a stream of numbers that “looks random”
  2. A mathematical function that converts uniform random variable(s) into random variables with other distributions

Mathematicians are mostly interested in engineering Step 2. Step 1 is important but is usually delegated to computer engineers.

With that preamble, here’s the question: Generate points randomly inside a triangle. I need this in order to play around with Sperner’s lemma examples.

The first step is to generate random points in the “standard triangle” with vertices \([0, 0], [1, 0], [0, 1]\). A very simple trick for this is to generate points in the unit square and “reflect the points” across the midpoint of the hypotenuse, \([0.5, 0.5]\).

Next, we use the barycentric transformation to transform the standard triangle into the desired triangle—in the example above, the equilateral triangle with vertices \([0, 0], [1, 0], [0.5, \sqrt{3}/2]\).

Back to top