- 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" ?.
How to generate random points on a sphere?
49.5k Views Asked by Bumbble Comm https://math.techqa.club/user/bumbble-comm/detail AtThere are 6 best solutions below
On
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
- Generate three random numbers $x, y, z$ using Gaussian distribution
- 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$.
- Multiply each number by the radius of your sphere.
On
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.
On
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.
On
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
$$ \phi = \arccos (2u_1-1)-\frac{\pi}{2} $$
and the longitude
$$ \lambda = 2\pi u_2 $$
Then compute the rectangular coordinates accordingly:
$$ x = \cos\phi\cos\lambda $$ $$ y = \cos\phi\sin\lambda $$ $$ z = \sin\phi $$
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$.
ETA (2023-10-11 thanks to Silverfish): I had originally swapped the usual symbols for latitude and longitude; this is now fixed.
On
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$.
Same way as on a real sphere, but $(x,y,z) $ multiplied by $i.$