Here I will only consider pseudo-random point generation. Well-spaced point generation is a different topic. The code examples are intended for clarity.

Uniform points on the unit disc

One method to generate a uniform random point $p$ on the unit disc ($\mathbb{D}$) is as follows. Generate two uniform values: $a \in \left[ 0,1 \right] $ , $ \theta \in \left[ -\pi, \pi \right) $ and apply1:

Using symmetry and trig identities we can drop this down to one trig approximation and pair of square roots.

Rejection method

Choose a 2D point $p$ uniformly in each dimension: $p_x,p_y \in \left[ -1,1 \right] $ and reject values when outside the unit disc. The average number of iterations is $\frac{4}{\pi} \approx 1.27324$. Although previously known the first CS publication of this technique seems to be von Neumann 19512.

An example implementation that returns $p \cdot p$ as well as the generated point:

float uniform_disc(vec2_t* p)
  float d,x,y;

  do {
    // lose one or both remaps (2*rng_f32()-1)
    // for uniform half or quarter disc
    x = 2.f*rng_f32()-1.f;    // (-1,1)
    y = 2.f*rng_f32()-1.f;    // (-1,1)
    d = x*x + y*y;            // p.p
  } while(d >= 1.f);          // branch prob: 1-pi/4 ~= 0.214602

  p->x = x;
  p->y = y;

  return d;                   // p.p

By dropping one of the range remapping of $x$ or $y$ gives a uniform distribution on the half disc and dropping both the quarter disc. The efficiency of the these are identical to the full disc case.

Ad-hoc non-uniform

If we rescale the component by some function of the dot product $d = (p \cdot p)$ we can get radial ad-hoc non-uniform distributions.

Directly scaling by the dot-product:

Scaling by $\frac{d}{1+d}$:

Uniform points on unit circle or uniform 2D rotation

Like the disc case we can generate a uniform point on the unit circle (1-sphere or $\mathbb{S}^1$) using trigonometry and symmetry/identities to reduce runtime complexity.

In the same paper2 as the disc rejection method John von Neumann presented a pair of techinques to transform uniform points on the unit disc to uniform points on the circle. If we have a uniform point $p = \left(x,y \right)$ (considered as a complex number) on the unit disc, then the first method is to simply normalize:

The second method observes that squaring drops the square root and since it doubles the angle maintains uniformity.

The second method is widely known since it trades a square root for two products and two additions which historically was an assured win. With the introduction of fast inverse square root approximations the lesser known normalization method becomes interesting. Also choosing between zero, one or two NR iterations give a knob of speed vs. bias of the result.

The remaining issue with a practical implementation is the degenerate case. I’ll assume that uniform float values are generated by “equidistance” method (pretty much the only one used). This method will generates values: $\frac{u}{2^{24}}$ where $u$ is a uniform integer on $\left[ 0, 2^{24} \right)$. The only problem result is then when both are zero. We can correct inverse square root by introducing a bias to the dot product which is sufficiently small that only contributes when both are zero such as $2^{-72}$. We also need to project that input to unit circle which requires choosing the branch point for the origin. Let’s send it to $(1,0)$ which needs adding a bias to the $x$ coordinate. This gives us:

#define BIAS (1.f/68719476736.f) // 0x1p-36f

void uniform_s1(vec2_t* p)
  float d = uniform_disc(p);
  float s = rsqrt(d+(BIAS*BIAS));  // 1/sqrt approx + X NR steps
  p->x += BIAS;                    // map (0,0) -> (1,0)
  p->x *= s;
  p->y *= s;

Since we’re simply normalizing we can create half and quarter circles as per same disc above.

Ad-hoc non-uniform

Taking the uniform variants and applying some simple transforms can yield some potentially interesting non-uniform distributions. If we take a uniform disc, translate it by one in x and then normalize we get a distribution that is roughly normal (standard deviation ~0.615113) on $\left[-\frac{\pi}{2},\frac{\pi}{2}\right] $ Using the angle-doubling version, well, doubles the angle output range.

Uniform points on the 3D unit sphere

In 1972 George Marsaglia3 introduced a method to transform a uniform point on the unit disc to a uniform point on the 3D sphere $\left(\mathbb{S}^2\right)$:

I walked through to this equation in the post “Implied normals (unit bivectors as rotations)”4 where it’s described as the sequence: orthogonal project disc to half-sphere followed by a double-angle transform. Some bullet points are:

  • an area-preserving map from disc to sphere (equivalent to Lamberts)
  • radial lines (through origin) on disc are mapped to longitude circles on sphere (angle measure maintained)
  • circles are mapped to latitudes (see below)
  • $\left( 0, ~0 \right) $ is mapped to the north pole
  • the disc: $r \in \left[ 0, ~\frac{1}{\sqrt{2}} \right] $ is mapped to the positive half sphere $\left( \mathbf{z} \geq 0 \right) $
  • the annulus: $r \in \left( \frac{1}{\sqrt{2}}, ~1 \right) $ is mapped to the negative half sphere $\left( \mathbf{z} < 0 \right )$
  • all points on the unit circle are mapped to south pole

void uniform_s2(vec3_t* p)
  float d,s;
  vec2_t v;

  d = uniform_disc(&v);
  s = 2.f*sqrtf(1.f-d);
  p->x = s*v.x;
  p->y = s*v.y;
  p->z = 1.f-2.f*d;

Given how points are mapped from the disc to the sphere, if we knew what “what circle maps to which latitude” we could form rotationally symettric distributions on the sphere directly from those on the disc. Dropping the angle-doubling map we end up with a cosine distribution on the half-sphere.

Spherical caps

As with non-uniform distributions, given the way the disc is mapped to the sphere we can directly formulate uniform points on spherical caps. The areas of the disc, sphere and spherical cap (height $h$) are respectively:

So the mapping from disc to sphere involves a constant factor of four and the area of the cap is linear with height. So we can form a point set on the unit disc, apply a uniform scaling and apply the disc to sphere mapping. The cap has a radius of one, divide out the constant factor of the disc to sphere map and relate the areas:

Solving for $r$ give us:

Plugging this scale factor into the map gives us the direct formulation:

void uniform_s2_cap(vec3_t* p, float h)
  float k,s;
  vec2_t v;

  k = h*uniform_disc(&v);   // h(x^2+y^2)
  s = sqrtf(h*(2.f-k));     // sqrt( h(2-h(x^2+y^2)) )
  p->x = s*v.x;
  p->y = s*v.y;
  p->z = 1.f-k;

Extra examples

Started this section to be able to add extra examples.

Cosine distribution on half-circle

Generating a uniform line and projecting to circle results in $P(\theta) = \frac{1}{2}\cos(\theta)$ with $\theta \in \left[\frac{\pi}{2}, \frac{\pi}{2} \right]$

void cosine_s1(vec3_t* p)
  float y = 2.f*rng_f32()-1.f;    // (-1,1)
  float x = sqrtf(1.f-y*y);
  p->x = x;
  p->y = y;

Squaring this result converts the half-circle to circle with: $P(\theta) = \frac{1}{4}\cos(\frac{\theta}{2})$

Cosine distribution on half-sphere

Seemingly the most common fragment shader version is as follows:

vec3 cosDistHalfSphere(in vec3 n , in float seed) 
  float d2 = rng(seed++);
  float a  = 2.0*PI*rng(seed++));

  // generate a uniform point on disc (trig method)
  // p = ra(rx,ry)
  float ra = sqrt(d2);
  float rx = cos(a); 
  float ry = sin(a);

  // project to sphere (Z)
  float rz = sqrt(1.0-d2);
  // an orthonormal basis (complement of 'n')
  vec3  xp = normalize(cross(n, vec3(0,1,1)));
  vec3  yp = cross(xp, n);

  // map 'p' to generated basis: ra*(rx*xp + ry*yp)
  // map 'z' to 'n':             rz*n
  return normalize(ra*(rx*uu + ry*vv) + rz*n);

Note that the basis computation is degenerate as ‘n’ approaches $\pm \frac{1}{2}\left(0, \sqrt{2}, \sqrt{2}\right)$ and this requires the otherwise unneeded normalization of the final result to reduce the degenerate zone.

So one option is use a method without a degenerate case5:

vec3 cosDistHalfSphere(in vec3 n , in float seed) 
  Float d2 = hash(seed++);
  float t  = 2.0*PI*hash(seed++);
  float ra = sqrt(d2);
  float rx = cos(t); 
  float ry = sin(t);
  float rz = sqrt(1.0-d2);
  // some alternate non-degenerate method
  // see post including links
  float sz = n.z >= 0.0 ? 1.0 : -1.0;
  float a  =  n.y/(1.0+abs(n.z));
  float b  =  n.y*a;
  float c  = -n.x*a;
  vec3  xp = vec3(n.z+sz*b, sz*c, -n.x);
  vec3  yp = vec3(c, 1.0-b, -sz*n.y);
  return ra*(rx*xp + ry*yp) + rz*n;

Another option if the direction will never approach some fixed (say negative $z$) is the method described by Colin Barre-Brisebois and Stephen Hill6.

References and Footnotes

  1. “Disk Point Picking”, Eric W. Weisstein, MathWorld 

  2. “Various Techniques Used in Connection With Random Digits”, John von Neumann, 1951 (PDF 2

  3. “Choosing a Point from the Surface of a Sphere”, George Marsaglia, 1972 (PDF

  4. “Implied normals (unit bivectors as rotations)”, (local post

  5. “Orthonormal basis from normal via quaternion similarity”, (local post

  6. “Blending in Detail”, Colin Barre-Brisebois and Stephen Hill, 2012 (link