For many applications in simulation, dynamical systems, robotics, machine learning, etc., we need random vectors that are uniformly distributed in multidimensional spheres. The number of dimensions can be very large, ruling out rejection methods (generate randoms in a d-cube, then filter out points not in the sphere). We exhibit two efficient methods, one geometric and one statistical. The statistical method is proved by invoking an ancient and beautiful theorem attributed to Archimedes.

Abstract

Can we generate spherically symmetric uniform randoms starting with rectangularly symmetric, uniform randoms? Yes, but it takes care to do it efficiently. Rejection methods — generating rectangularly symmetric, uniform randoms and then throwing out those that are not in the
d
-ball — are crippled: only one in a trillion endpoints in the 27-cube is also in the 27-ball; the ratio gets worse exponentially as
d
increases. We need spherically symmetric uniform randoms in the thousands of dimensions for robots or even in the billions of dimensions for neural networks.
We exhibit two computational methods — one geometrical, the other statistical — that scale efficiently with
d
for generating pseudorandom vectors uniformly and symmetrically on the surface and in the interior of the unit hypersphere of dimension
d
.
Though no method here is novel, descriptions available to me are unsatisfactory. The contributions of this note are proofs, executable specifications of algorithms, and numerical demonstrations.

Definitions

The unit
d
-ball is the interior of a hypersphere, hyper meaning of any dimension. The unit
d
-ball is the set of all real
d
-vectors rooted at the origin,
(
x
1
,
x
2
,…,
x
d
)∈
d

, such that
2
x
1
+
2
x
2
+⋯+
2
x
d
<1
. The unit
d
-sphere is the boundary of the
d
-ball, that is, the set of all real
d
-vectors such that
2
x
1
+
2
x
2
+⋯+
2
x
d
=1
.
The unit
d
-cube is the set of all real
d
-vectors such that each coordinate is absolutely bounded by unity, inclusively, that is, where
-1≤
x
i∈[1..d]
≤1
. The unit
d
-ball,
d
-sphere, and
d
-cube are also said to have radius 1, diameter 2.
We do not distinguish between the mathematical procedure of sampling a distribution and the computational procedure of generating pseudorandoms according to a distribution. Likewise, we do not distinguish between mathematical true randoms and computational pseudorandoms.
Generating randoms uniformly means the same as generating uniform randoms. Generating randoms symmetrically means the same as generating symmetric randoms. Symmetrically means independently of geometrical direction.

Uniform Randoms in the
d
-Cube

Given an efficient method for generating uniform random scalars in the real interval
[-1..1]
— in the 1-cube — it is easy and efficient to generate rectangularly symmetric, uniform randoms in the
d
-cube: generate randoms for each of the
d
coordinate components independently.
Can we generate spherically symmetric uniform randoms starting with rectangularly symmetric, uniform randoms? Yes, but it takes care to do it efficiently. Rejection methods — generating rectangularly symmetric, uniform randoms and then throwing out those that are not in the
d
-ball — are crippled: only one in a trillion endpoints in the 27-cube is also in the 27-ball; the ratio gets worse exponentially as
d
increases.
Consider points (vector endpoints) uniformly distributed in the unit 2-cube, i.e., the unit square. The resulting
(x,y)
pairs are not uniform with angle, therefore not spherically symmetric. The following diagrams illustrate: here are points uniformly distributed in the 2-cube:
Out[]=
There are too many points in the corners, at
ArcTan[1,1]=0.7854
,
ArcTan[-1,1]=2.3562
and their negatives:
Out[]=

Rejection Method: Impractical

We could reject, i.e., remove points in the unit
d
-cube that are not in the unit
d
-ball. Those would be points with radial component — Euclidean norm — greater than 1. However, rejection does not scale. As
d
grows, the number of corners goes up as
d
2
and almost all points are in the corners, outside the
d
-ball. To see this this, consider the ratio of the volume of a unit
d
-ball to the volume of a unit
d
-cube.
What is the volume of a unit
d
-cube? Answer:
d
2
. What is the volume of a unit
d
-ball? (see https://en.wikipedia.org/wiki/Volume_of_an_n-ball)
Answer for integer
d
:
In[]:=
ClearAll[vBall];​​vBall[d_,r_:1]:=
With{k=d/2},
k
π
k!
d
r

Mod[d,2]===0
Withk=
d-1
2
,
2k!
k
(4π)
d!
d
r

Mod[d,2]===1
;​​Table<|"dimension"d,"ball volume"vBall[d],"cube volume"
d
2
,"cube/ball ratio"N
d
2
vBall[d]|>,{d,1,10}//MatrixForm
Out[]//MatrixForm=
dimension1,ball volume2,cube volume2,cube/ball ratio1.
dimension2,ball volumeπ,cube volume4,cube/ball ratio1.27324
dimension3,ball volume
4π
3
,cube volume8,cube/ball ratio1.90986
dimension4,ball volume
2
π
2
,cube volume16,cube/ball ratio3.24228
dimension5,ball volume
8
2
π
15
,cube volume32,cube/ball ratio6.07927
dimension6,ball volume
3
π
6
,cube volume64,cube/ball ratio12.3846
dimension7,ball volume
16
3
π
105
,cube volume128,cube/ball ratio27.0913
dimension8,ball volume
4
π
24
,cube volume256,cube/ball ratio63.0742
dimension9,ball volume
32
4
π
945
,cube volume512,cube/ball ratio155.222
dimension10,ball volume
5
π
120
,cube volume1024,cube/ball ratio401.543
for arbitrary real dimensions, the plot must be logarithmic to be useful. Notice the volume ratio is about
-12
10
for
d=27
.
In[]:=
ClearAll[vBall];​​vBall[d_,r_:1]:=
d/2
π
d
r
Gamma[1+d/2]
;
Out[]=
The ratio of ball-to-cube volume in 27 dimensions is one trillionth, so the rejection method generates a trillion times more points than it keeps. The computer does 1 trillion times more work than it ought to do. Rejection is impractical for even moderately large
d
.

Radial Symmetry

Observe that a multivariate Gaussian or multinormal distribution is radially symmetric. Sampling
x
from
-
2
x
2

2π
, and
y
similarly, then noticing that
x
and
y
are independent (the probability of their occurring simultaneously is the product of their individual probabilities), we get
-
2
x
2

2π
×
-
2
y
2

2π
=
-
1
2

2
x
+
2
y


2π
. A similar construction extends to any number of dimensions.
The multinormal distribution is radially symmetric because it depends only on the radius squared,
2
x
+
2
y
, not on the azimuth angle. But it is not radially uniform, as the following figure demonstrates:
In[]:=
show2D$[RandomVariate[​​
MultinormalDistribution[IdentityMatrix[2]]
,40000],2.5]
Out[]=

Radial Uniformity

From Symmetry to Uniformity

Efficient Geometrical Construction

sampleDBall

The following plots illustrate points uniformly distributed in radius in two and three dimensions, and thus uniformly distributed in the 2-ball and in the 3-ball, respectively:

Uniformity Statistics

It’s well and good to appear to be uniform, but we must demonstrate uniformity with statistics.

Shell Statistics

A rigorous Chi-squared test would give the probability that each shell is not drawn from the same uniform distribution. The “eyeball” agreement between empirical and expected standard deviations is good enough that a rigorous Chi-squared test does not seem worth the effort.

Sampled Shell Counts

Two Dimensions

Three Dimensions

Six Dimensions

Sixteen Dimensions

276 Dimensions

Cubie Statistics

Two Dimensions

There are already, even in two dimensions, a noticeable number of deficiently populated cubies (the tail to the left of the peak above). These are the cubies crossing the circle and in the corners outside the circle.

Three Dimensions

In three dimensions, the number of deficient cubies is larger than “noticeable.”

Six Dimensions

In six dimensions, the deficient cubies dominate the statistics.

Twenty-Seven Dimensions

In twenty-seven dimensions, virtually all the cubies are deficient.

Efficient Statistical Construction

Why does this work? Why reject two coordinates at random, instead of one or three or seventeen?

Theorem 1

Proof

We prove the theorem first for three dimensions, then argue by extension to dimensions greater than three.
As before, generate spherically uniform 3-vectors of unit length — on the 3-sphere — by sampling the 3-dimensional multinormal distribution, then normalizing the vectors:
What is the 2D distribution at this step?

Lemma 1

The following illustrates five belts in 3D:
Proof of the Hatbox Theorem
QED Hatbox Theorem ■
We claim that each annulus contains the same number of points, statistically.
The following illustrates that all annuli have, statistically, the same numbers of points.
QED Lemma 1 ■
Therefore each band has the same density.

Two Dimensions

276 Dimensions

QED Theorem 1 ■

CITE THIS NOTEBOOK

Archimedes' Hatbox — Hyperspherical Pseudorandom Vectors​
by Brian Beckman​
Wolfram Community, STAFF PICKS, December 11, 2023
​https://community.wolfram.com/groups/-/m/t/3081911