Uniform Sampling from the 3D Unit Ball

Random Variables · Medium · Free problem

You want to generate points that are uniformly distributed over the interior of a 3D unit sphere -- meaning every sub-volume of equal size is equally likely to contain a point.

  1. Describe two methods to do this: rejection sampling and the inverse CDF (direct) method. For each, explain the procedure and, for rejection sampling, compute the acceptance rate.
  1. For a point $(X, Y, Z)$ drawn uniformly from the ball, derive the marginal PDF of a single coordinate -- say $X$. What does it look like, and where is the density concentrated?

Hints

  1. For rejection sampling, think about what fraction of the bounding cube $[-1,1]^3$ is actually inside the unit ball -- that gives you the acceptance rate directly.
  2. For the direct method, figure out how radius $R$ must be distributed: in 3D, volume scales as $r^3$, so the CDF of $R$ is $F_R(r) = r^3$. Invert this to sample $R$, and use a normalized Gaussian vector for the direction.
  3. For the marginal of $X$: at a fixed $x$, the valid $(y, z)$ pairs fill a disk of radius $\sqrt{1 - x^2}$. The PDF $f_X(x)$ is proportional to the area of that disk, $\pi(1 - x^2)$, normalized over $[-1, 1]$.

Worked Solution

How to Think About It: Uniform sampling from a 3D ball trips people up because the naive approach fails: if you just pick $R \sim \text{Unif}(0,1)$ and a random direction, you over-sample near the center. The key issue is that volume grows as $r^2 \, dr$ in 3D -- there is far more volume near $r = 1$ than near $r = 0$. So $R$ must have a distribution that accounts for this volume weighting. Two practical methods: rejection sampling (simple to implement, decent efficiency) and the inverse CDF method (exact, no wasted samples). For the marginal, think about how many points with $x$-coordinate near some value $x$ exist -- you are integrating over a disk of radius $\sqrt{1 - x^2}$, so the density is proportional to the area of that disk.

Quick Estimate: Rejection sampling acceptance rate: we are drawing from a cube $[-1,1]^3$ (volume 8) and keeping points inside the unit ball (volume $\frac{4}{3}\pi$). Acceptance rate $= \frac{4\pi/3}{8} = \frac{\pi}{6} \approx 0.524$. So roughly half your samples are accepted -- quite efficient for 3D. In $d$ dimensions this degrades fast: at $d = 10$, the ball fills only about 0.25% of the bounding hypercube, making rejection sampling essentially useless.

Formal Solution:

Method 1 -- Rejection Sampling:

  1. Draw $(x, y, z) \sim \text{Unif}[-1, 1]^3$ (three independent uniform draws on $[-1, 1]$).
  2. Accept if $x^2 + y^2 + z^2 \leq 1$; otherwise reject and repeat.

The volume of the cube is

^3 = 8$. The volume of the unit ball is $\frac{4}{3}\pi$. Acceptance probability:

$p_{\text{accept}} = \frac{4\pi/3}{8} = \frac{\pi}{6} \approx 52.4\%$

Conditional on acceptance, the distribution is exactly uniform over the ball by symmetry.

Method 2 -- Inverse CDF (Direct Method):

First, find the CDF of the radial coordinate $R$ for a uniform ball point. In 3D, volume scales as $r^3$, so:

$F_R(r) = r^3, \quad r \in [0, 1]$

Inverting: if $U \sim \text{Unif}(0, 1)$, set $R = U^{1/3}$.

For the direction, draw $(G_1, G_2, G_3) \stackrel{\text{iid}}{\sim} N(0, 1)$ and normalize to get a uniform point on the sphere surface. Then the final point is:

$\mathbf{p} = U^{1/3} \cdot \frac{(G_1, G_2, G_3)}{\|(G_1, G_2, G_3)\|}$

This produces exactly one accepted point per draw -- no waste.

Part 2 -- Marginal Distribution of $X$:

For a fixed value $x \in [-1, 1]$, the cross-section of the ball perpendicular to the $x$-axis is a disk of radius $\sqrt{1 - x^2}$ and area $\pi(1 - x^2)$. The PDF of $X$ is proportional to this cross-sectional area:

$f_X(x) = \frac{\pi(1 - x^2)}{\int_{-1}^{1} \pi(1 - t^2) \, dt}$

The integral in the denominator:

$\int_{-1}^{1} (1 - t^2) \, dt = \left[t - \frac{t^3}{3}\right]_{-1}^{1} = \frac{4}{3}$

So:

$\boxed{f_X(x) = \frac{3}{4}(1 - x^2), \quad x \in [-1, 1]}$

Answer:

Intuition

The core lesson here is that uniform over a ball is not the same as uniform in each coordinate independently, and it is not uniform in the radius either. Volume in 3D accumulates as $r^2 \, dr$, so the outer shell near $r = 1$ contains far more volume than the center -- which is why you must transform a uniform $U$ into $R = U^{1/3}$ rather than using $R = U$ directly. This is a specific case of the general principle: when sampling from a region in $d$ dimensions, you need to account for how the "measure" (volume, area) changes with each coordinate.

The marginal distribution result -- $f_X(x) = \frac{3}{4}(1 - x^2)$ -- shows up naturally when you think about cross-sections. It is not uniform, and it is not Beta in the standard sense, but it has the same qualitative shape: density peaks at the center ($x = 0$) and tapers off toward the edges. This matters in practice: if you are working with high-dimensional data on or near a sphere (common in machine learning and factor models), the projection of your data onto any single axis will look like this distribution, not a flat one. Understanding geometric distortion from high-dimensional spaces is directly relevant to portfolio risk modeling, factor decompositions, and random matrix theory.

Open the full interactive solver →