I would like to generate a random axis or unit vector in 3D. In 2D it would be easy, I could just pick an angle between 0 and 2*Pi and use the unit vector pointing in that direction.

But in 3D I don't know how can I pick a random point on a surface of a sphere.

If I pick two angles the distribution won't be uniform on the surface of the sphere. There would be more points at the poles and less points at the equator.

If I pick a random point in the (-1,-1,-1):(1,1,1) cube and normalise it, then there would be more chance that a point gets choosen along the diagonals than from the center of the sides. So thats not good either.

But then what's the good solution?


5 Answers 5


You need to use an equal-area projection of the sphere onto a rectangle. Such projections are widely used in cartography to draw maps of the earth that represent areas accurately.

One of the simplest such projections is the axial projection of a sphere onto the lateral surface of a cylinder, as illustrated in the following figure:

Cylindrical Projection

This projection is area-preserving, and was used by Archimedes to compute the surface area of a sphere.

The result is that you can pick a random point on the surface of a unit sphere using the following algorithm:

  1. Choose a random value of $\theta$ between $0$ and $2\pi$.

  2. Choose a random value of $z$ between $-1$ and $1$.

  3. Compute the resulting point: $$ (x,y,z) \;=\; \left(\sqrt{1-z^2}\cos \theta,\; \sqrt{1-z^2}\sin \theta,\; z\right) $$

  • 1
    $\begingroup$ This seems incorrect regarding #2. If $z$ is uniformly distributed between $-1$ and $1$, it will bias towards the poles. According to mathworld.wolfram.com/SpherePointPicking.html, "pick $z = \cos \phi$ to be uniformly distributed (so that we have $dz = \sin \phi \, d \phi$)". $\endgroup$ Commented Mar 30, 2016 at 18:13
  • 6
    $\begingroup$ @ɲeuroburɳ It is in fact correct. The $z$-value of a point on the unit sphere is uniformly distributed between $-1$ and $1$. Indeed, this is precisely what the sentence that you quote from the mathworld article is saying. $\endgroup$
    – Jim Belk
    Commented Mar 31, 2016 at 3:47
  • $\begingroup$ Is there somewhere I can find a proof of this? $\endgroup$
    – ions me
    Commented Jun 2, 2020 at 1:51
  • $\begingroup$ @IonSme In general the area element for a surface of revolution is $dA = r\sqrt{1+\biggl(\dfrac{dr}{dz}\biggr)^2}\,dz\,d\theta$. For the unit sphere we have $r=\sqrt{1-z^2}$, and substituting this in gives $dA=dz\,d\theta$. $\endgroup$
    – Jim Belk
    Commented Jun 2, 2020 at 10:19

Another commonly used convenient method of generating a uniform random point on the sphere in $\mathbb{R}^3$ is this: Generate a standard multivariate normal random vector $(X_1, X_2, X_3)$, and then normalize it to have length 1. That is, $X_1, X_2, X_3$ are three independent standard normal random numbers. There are many well-known ways to generate normal random numbers; one of the simplest is the Box-Muller algorithm which produces two at a time.

This works because the standard multivariate normal distribution is invariant under rotation (i.e. orthogonal transformations).

This has the nice property of generalizing immediately to any number of dimensions without requiring any more thought.

  • $\begingroup$ Isn't this the same as my second idea of picking a point inside the (-1,-1,-1):(1,1,1) cube and normalizing it? $\endgroup$
    – hyperknot
    Commented Jun 11, 2011 at 4:15
  • 12
    $\begingroup$ @zsero: No, the multivariate normal distribution is spherically symmetric, so this method won't suffer the diagonals problem of the cube method. $\endgroup$
    – Jim Belk
    Commented Jun 11, 2011 at 5:22
  • $\begingroup$ This method is discussed in this paper by M.E. Muller. Additionally, Marsaglia's ziggurat algorithm could also be used to generate the normal random deviates. $\endgroup$ Commented Aug 15, 2012 at 0:04

You can also do this. Generate three random numbers $(a,b,c)$ in $[-1,1]$; if $a^2 + b^2 + c^2\le 1$, then normalize them. Otherwise try again and pick triplets until you have a usable triplet. The volume of the cube we pick from is 8. The volume of the unit ball is $4/3\pi$, so typically you will choose roughly two triplets to get one good random vector on the sphere.

  • $\begingroup$ I see your point, but do you think the distribution would be uniform on the surface? $\endgroup$
    – hyperknot
    Commented Jun 20, 2011 at 11:24
  • $\begingroup$ Yes. You only normalize points inside of the unit ball and reject those outside. It is spherically symmetric. $\endgroup$ Commented Jun 20, 2011 at 13:36
  • 13
    $\begingroup$ This works, but in high dimensional space this will require a very large number of random number generations before x_1^2 + x_2^2 + x_3^2 + ... + x_n^2 <= 1 is true. $\endgroup$
    – Jason
    Commented Jul 7, 2012 at 0:13
  • 4
    $\begingroup$ Indeed true. The volume of the unit sphere shrinks fast in high dimesions, but the OP only was interested in the three-dimensional case. $\endgroup$ Commented Jul 25, 2015 at 20:12

Courtesy of the total Compendium from computer graphics, item (33) on page 19:

In spherical coordinates, set:

$$ r = \text{radius} $$

$$ \theta = \arccos( 1 - 2\zeta_1 ) $$

$$ \phi = 2 \pi \zeta_2 $$

Where $\theta$ is the inclination angle (measured from the zenith) and $\phi$ is the azimuthal angle (measured from the x-axis). That is,

$$x = r \cos \phi \sin \theta$$

$$y = r \sin \phi \sin \theta$$

$$z = r \cos \theta$$

$\zeta_1$ is a uniformly distributed random variable on $[0,1]$. $\zeta_2$ is another uniformly distributed random variable on $[0,1]$.

enter image description here

  • 2
    $\begingroup$ The $\xi$ in the expression for $\theta$ and the one in the expression for $\phi$ need to be two different independent random variables (otherwise you'll get a spiral). After that, this becomes equivalent to Jim Belk's answer. $\endgroup$
    – user856
    Commented Aug 14, 2012 at 23:11
  • $\begingroup$ Yes, they have to be different. I'll clarify that with subindices $\endgroup$
    – bobobobo
    Commented Aug 15, 2012 at 3:43

George Marsaglia, in this paper, gives the following proposal:

Keep generating independent random values $v_1$ and $v_2$ in $(-1,1)$ until $s=v_1^2+v_2^2 < 1$, then the random point on the sphere is formed as $(2v_1\sqrt{1-s},2v_2\sqrt{1-s},1-2s)$

See the paper for details on how it works.

  • $\begingroup$ If we sample z uniformly and don't take into account the factor sqrt(1-z^2) in the surface element then we would get too many samples at the poles. That's why the first answer given is valid $\endgroup$
    – Anna Naden
    Commented Oct 16, 2022 at 4:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .