# How to generate random points on a sphere?

• How do I generate $$1000$$ points $$\left(x, y, z\right)$$ and make sure they land on a sphere whose center is $$\left(0, 0, 0\right)$$ and its diameter is $$20$$ ?.
• Simply, how do I manipulate a point's coordinates so that the point lies on the sphere's "surface" ?.

Using Gaussian distribution for all three coordinates of your point will ensure an uniform distribution on the surface of the sphere. You should proceed as follows

1. Generate three random numbers $x, y, z$ using Gaussian distribution
2. Multiply each number by $1/\sqrt{x^2+y^2+z^2}$ (a.k.a. Normalise) . You should handle what happens if $x=y=z=0$.
• Excellent! This works in any dimension and seems not to be widely known, although I've seen some older paper (late $'40$'s) where the authors claim that they've learned it from Harald Cramér. Dec 22, 2015 at 22:43
• Not widely known? Every probabilist should know that the multivariate standard normal distribution is spherically symmetric, from which this follows immediately. Dec 23, 2015 at 0:40
• Does it have to be a Gaussian distribution? Does the standard deviation of the distribution matter? What if I were to use a uniform distribution? Jul 6, 2019 at 3:19
• @NicNic8 It has to be spherically symmetric, so for example the density (before adjusting for the magnitude) at $(1.25,0,0,0)$ has to be the same as the density at $(0,0.75,1,0)$ and a multivariate Gaussian distribution with covariance matrix which is a constant times the identity matrix achieves this. The standard deviation does not matter so long as it is positive. A uniform distribution on say $[0,1]$ would not therefore not work Jul 6, 2019 at 11:04
• @Henry's comment is correct. Intuitively, you can reason about this in 2D. Using the above technique, any sample that you take that occurs along the same radial line will be mapped to the same point on the perimeter of the unit circle. Since the Gaussian has an identity covariance (which can be scaled by a constant), the area along any radial line from 0 to infinity is the same. Note that this is not true if the Gaussian is not symmetric. Hence, each point on the perimeter of the circle has an equal probability of being generated. Therefore, samples on the perimeter are uniformly distributed. Sep 21, 2019 at 18:01

Use the fact that if you cut a sphere of a given radius with two parallel planes, the area of the strip of spherical surface between the planes depends only on the distance between the planes, not on where they cut the sphere. Thus, you can get a uniform distribution on the surface using two uniformly distributed random variables:

• a $z$-coordinate, which in your case should be chosen between $-10$ and $10$; and
• an angle in $[0,2\pi)$ corresponding to a longitude.

From those it’s straightforward to generate the $x$- and $y$-coordinates.

• This always challenges my intuition. Dec 22, 2015 at 21:17
• So you should get a uniform distribution on the cylinder and project on the sphere, like Archimedes. Nice! Dec 22, 2015 at 22:37
• This proves, by the way, that the surface area of a sphere's circumscribing cylinder (minus the endcaps) equals that of the sphere itself (and by corollary, the volume of the sphere is one-third the radius times the common surface area). Dec 23, 2015 at 0:11
• Does this generalize to higher dimensions? For a 2D circle you just pick $\theta$ uniformly. For a 3D sphere you choose a $\theta$ and $z$. Is there a similar result for 4D and higher? Dec 23, 2015 at 13:07
• While this method is simple and clever, it can suffer from numerical accuracy issues near the poles (where ${\rm d}x/{\rm d}z \to \infty$). For example, let's assume that your random number generator internally generates a 32-bit integer $n$, which is then scaled to the desired interval as $z = 20n/2^{32}-10$. Thus, the minimum distance between $z$ values is $20/2^{32}\approx5\times10^{-9}$, while near the poles, the minimum distance between points along a line of longitude is $10\arccos(1-2/2^{32})\approx0.0003$. For a sphere the size of the Earth, that distance would be about 200 meters. Dec 23, 2015 at 19:04

Here is a simple but less efficient way:

Generate points uniformly $x \in [-10,10]^3$ and reject if $\|x\| =0$ (which should rarely happen) or $\|x\| > 10$ (which should happen with probability ${20^3 -{4 \over 3} \pi 10^3 \over 20^3} =1 - {\pi \over 6} \approx 48\%$). Otherwise let $y = {10 \over \|x\|} x$. Then $y$ will be distributed uniformly on the surface of the $10$-sphere.

• The points that are not rejected are uniformly distributed in the sphere, and projecting such points to the surface preserves uniformity. Dec 22, 2015 at 22:48
• This method is probably faster than the others from a computationnal point of view. The only complex operations are a division and a square root, and both are quite fast Dec 23, 2015 at 21:34
• I think for 1000 points the cost is minimal, and the coding fairly simple. Dec 23, 2015 at 22:13
• @heropup : yes, in high dimensions the rejection rate is big, and the gaussian method scale lineary contrary to this method. But in low dimensions, it works very well Dec 23, 2015 at 22:30
• For those implementing this algorithm, you can improve performance by removing the square root operation and comparing the squared length of vector x with the squared radius of the circle. Aug 26, 2022 at 22:50

In addition to Brian Scott's excellent and clever answer, here's another, more straightforward way (in case you want to approach it with a geographical intuition): From two random variables $u_1, u_2$, distributed uniformly on the interval $[0, 1]$, generate (in radians) the latitude

$$\lambda = \arccos (2u_1-1)-\frac{\pi}{2}$$

and the longitude

$$\phi = 2\pi u_2$$

Then compute the rectangular coordinates accordingly:

$$x = \cos\lambda\cos\phi$$ $$y = \cos\lambda\sin\phi$$ $$z = \sin\lambda$$

ETA (thanks to Tanner Strunk—see comments): This will give coordinates of points on the unit sphere. To have them land on the sphere with diameter $20$ (and therefore radius $10$), simply multiply each by $10$.

• This is the best way. Some points: 1. arccos(-1..1) normally gives the range 0..pi, so I would be inclined to subtract pi/2 instead of pi, to get a number in the range -pi/2 .. pi/2 which is how a geographer would present the angle. Actually I think this change is required, because the current answer will always give negative z. 2. Having done that, I see nothing wrong with z = 2u -1 and cos lamba = sqrt(1-z^2) 3. phi works just as well with 2*npiu2, which may be convenient in some computer implementations. Dec 24, 2015 at 11:06
• Oops, I think you are right. Thanks for the look out. Dec 24, 2015 at 15:41
• I'm a bit late in coming to this question (ha), but you should be scaling those x,y,z expressions by 10 to land on the sphere, right? (Tiny, nit-picky detail, but I figure it should be noted for future readers.) Nice avatar by the way, Brian. Jan 11, 2018 at 17:33
• @TannerStrunk: Yes, you're quite right, thanks! I'll add an edit. Jan 11, 2018 at 18:35
• Note that the latitude generation can be simplified to $\lambda = \arcsin(2a - 1)$. Oct 28, 2019 at 23:57

Same way as on a real sphere, but $(x,y,z)$ multiplied by $i.$

• I'm afraid the OP by "imaginary" just meant "mathematical" Dec 22, 2015 at 20:43
• That's funny! ${}{}$ Dec 22, 2015 at 20:54
• May have been edited on 23 March 2015 to remove "imaginary" Dec 23, 2018 at 10:38

Wolfram Mathworld provides a methodology for randomly picking a point on a sphere:

To obtain points such that any small area on the sphere is expected to contain the same number of points, choose $u$ and $ν$ to be random variates on $[0,1]$. Then: $$\begin{array}{ll}\theta=2\pi u\\ \varphi= arccos(2v - 1)\end{array}$$ gives the spherical coordinates for a set of points which are uniformly distributed over $\mathbb{S}^2$.

• Except for the link, this seems to be exactly the same as Brian Tung's earlier answer. Dec 23, 2015 at 18:39