# Given three random numbers between 0 and 1, how to evenly populate a sphere?

• I
• member 428835

#### member 428835

Hi PF!

Given three random numbers between 0 and 1, how to evenly populate a sphere of radius ##R## (assuming we use every point). I think it's similar to the 2D circle equivalent described here. Does this imply the PDF is ##4 x^2##, where the remaining analysis holds? Then one point is the polar angle, one is the azimuthal angle, and the last is the radius, which I guess is ##(3/4x)^{1/3}## where ##x## is the third random number generated.

The easiest and most intuitive way is to generate random points in a cube with coordinates from three random numbers between ##-R## and ##R## and reject those that are not inside the sphere. (I am assuming that you are including the interior and not talking only about points on the surface of a sphere.) It keeps everything simple.

Last edited:
The easiest and most intuitive way is to generate random points in a cube with coordinates from three random numbers between ##-R## and ##R## and reject those that are not inside the sphere. (I am assuming that you are including the interior and not talking only about points on the surface of a sphere.) It keeps everything simple.
Yea for sure, but I specified we must use every point (no rejections)

Yea for sure, but I specified we must use every point (no rejections)
Sorry, I missed that. Then this sounds more like an academic exercise. I can't imagine why else that would be a requirement. I think your adjustment for the radius is right.

Last edited:
Sorry, I missed that. Then this sounds more like an academic exercise. I can't imagine why else that would be a requirement. I think your adjustment for the radius is right.
I'm applying for jobs that have weird interview questions, and so I am going through books and online question banks. I found one regarding circles, where the pdf was ##2x## and was hoping I could extrapolate to the 3D question. So academic in nature, but not actually for a class.

• FactChecker
Would not a sphere encompassing a cube with the same center simply have a radius equal to the length of a line from the cube's center to it's corner?

Would not a sphere encompassing a cube with the same center simply have a radius equal to the length of a line from the cube's center to it's corner?
It would. But not sure this is relevant.

Hi PF!

Given three random numbers between 0 and 1, how to evenly populate a sphere of radius ##R## (assuming we use every point). I think it's similar to the 2D circle equivalent described here. Does this imply the PDF is ##4 x^2##, where the remaining analysis holds? Then one point is the polar angle, one is the azimuthal angle, and the last is the radius, which I guess is ##(3/4x)^{1/3}## where ##x## is the third random number generated.

I don't know if you only want to just its surface or solid sphere.

1. If surface, you can choose a point with R distance from coordinate (0,0,0). Then three random numbers are three angles. You can rotate coordinates along X, Y, and Z axes using randomly generated three angles. If you keep doing it, you will get the full surface of a sphere.

2. Solid sphere: choose a solid cube with a diagonal is 2R. The center of the cure is at (0, 0, 0). Again, you can rotate X, Y, Z axis with three random angles. You will get a solid sphere with a radius of R.

Of course, if the condition requires you can not start with a point or a cube, then these methods are not fit for you.

I found one regarding circles, where the pdf was ##2x## and was hoping I could extrapolate to the 3D question.
Yes, and you have done this correctly for the distance from the centre. The next step is to distribute uniformly over the surface of the sphere with radius at that distance; this is more complicated than uniform distribution of ## \theta \text { and } \varphi ## (note that on Earth the area enclosed by 1 degree of latitude and longitude is not uniform).

Last edited:
I don't know if you only want to just its surface or solid sphere.

1. If surface, you can choose a point with R distance from coordinate (0,0,0). Then three random numbers are three angles. You can rotate coordinates along X, Y, and Z axes using randomly generated three angles. If you keep doing it, you will get the full surface of a sphere.

2. Solid sphere: choose a solid cube with a diagonal is 2R. The center of the cure is at (0, 0, 0). Again, you can rotate X, Y, Z axis with three random angles. You will get a solid sphere with a radius of R.

Of course, if the condition requires you can not start with a point or a cube, then these methods are not fit for you.
Sorry, I'm not seeing how either approach works, though I like the situation 1) you've posed for getting the surface of a sphere; haven't thought of this one, so I should try solving it.

Yes, and you have done this correctly for the distance from the centre. The next step is to distribute uniformly over the surface of the sphere with radius at that distance; this is more complicated than uniform distribution of ## \theta \text { and } \varphi ## (note that on Earth the area enclosed by 1 degree of latitude and longitude is not uniform).
Are you sure? Perhaps I'm missing something big here. I am thinking for the asimuthal angle ##\theta \in [0,2\pi]## each degree is symmetric, so it doesn't seem like we'd be denser in the west than the east. Isn't the same tru for the polar angle ##\phi\in [0,\pi]##? It doesn't seem like you'd populate more of the northern than southern hemisphere.

EDIT: okay, nevermind, I see your point. Letting ##\phi=10^\circ## implies the ##\theta## "ring" is much smaller, and therefore denser than ##\phi=90^\circ##. But how do we account for this? I'm trying to think surface metrics here: given ##r## and ##\phi##, we have a circumference of length ##2\pi (r\sin\phi)## where ##\theta## then selects the specific location on. We somehow want to normalize the distribution of ##\phi## such that ##2\pi (r\sin\phi)## receives the same average points per length for all ##\phi##. So we want the distribution of ##\phi## to be more concentrated around ##\phi=90^\circ## and less concentrated around ##\phi = 0^\circ,180^\circ##.

I don't know why, but intuitively I'm thinking perhaps we distribute ##\phi## like a circle, so that ##\phi = 90^\circ - \sqrt{90^2-x^2}## if ##x \in [0^\circ,90^\circ]## and ##\phi = 90^\circ - \sqrt{90^2-(x-180)^2}## if ##x \in [90^\circ,180^\circ]## where ##x## is the random number normalized from 0 to ##2\pi##.

Last edited by a moderator:
The surface of a sphere is ##4\pi r^2##. Then if the radius is given by ##\sqrt x R## and the other 2 x's are angles, the density of points in ONLY radius as variablespace is ##dx/d\sqrt x = 3\sqrt x^2/R## at radius ##r=\sqrt x R##. Thus the density of points at radius r is ##3r^2 /R^3 \sim 4\pi r^2##. Then if we take angles from the other 2 random points such that a sphere's surface is covered homogeneously, the density at a point ##(r,\phi,\psi)## is the density ##3r^2 /R^3## calculated earlier divided by the surface of a sphere ##4\pi r^2## which is constant.

Checking that the volume of the sphere is ##(4/3)\pi R^3##, indeed multiplied by this constant it becomes 1: ##(4/3)\pi R^3 3r^2 /(R^3 4\pi r^2) = 1##.

The key is to consider the exponentials ##x=e^{-y}## and ##r/R = e^{-z}##. Now the fraction of volumes up to r versus R equals ##e^{-3z}##. You want to choose r such that this equals x, since that's the chance to get below x. Then of course ##y=3z## giving ##r/R=\sqrt{x}##

So we want the distribution of ##\phi## to be more concentrated around ##\phi=90^\circ## and less concentrated around ##\phi = 0^\circ,180^\circ##.
Yes that's right.

I don't know why, but intuitively I'm thinking perhaps we distribute ##\phi## like a circle, so that ##\phi = 90^\circ - \sqrt{90^2-x^2}## if ##x \in [0^\circ,90^\circ]## and ##\phi = 90^\circ - \sqrt{90^2-(x-180)^2}## if ##x \in [90^\circ,180^\circ]## where ##x## is the random number normalized from 0 to ##2\pi##.
I don't think intuition will help here*, not compared to either working it out properly or searching for a solution that already exists for a uniform distribution on the surface of a sphere.

Mathworld calls this Sphere Point Picking.

* I have noticed before that you tend to rely on what you call intuition too much: I would call this guessing. Guessing is OK, but once you have made a guess you need to try it to see if it works, not just throw it out there on a forum and expect someone else to try it for you.

* I have noticed before that you tend to rely on what you call intuition too much: I would call this guessing. Guessing is OK, but once you have made a guess you need to try it to see if it works, not just throw it out there on a forum and expect someone else to try it for you.
You don't know what work I've put into each problem before hand. If you don't like me posting, just don't answer. I have appreciated your help in the past; after reading your perspective, I can do without it.

Are you sure? Perhaps I'm missing something big here. I am thinking for the asimuthal angle ##\theta \in [0,2\pi]## each degree is symmetric, so it doesn't seem like we'd be denser in the west than the east. Isn't the same tru for the polar angle ##\phi\in [0,\pi]##? It doesn't seem like you'd populate more of the northern than southern hemisphere.

EDIT: okay, nevermind, I see your point. Letting ##\phi=10^\circ## implies the ##\theta## "ring" is much smaller, and therefore denser than ##\phi=90^\circ##. But how do we account for this? I'm trying to think surface metrics here: given ##r## and ##\phi##, we have a circumference of length ##2\pi (r\sin\phi)## where ##\theta## then selects the specific location on. We somehow want to normalize the distribution of ##\phi## such that ##2\pi (r\sin\phi)## receives the same average points per length for all ##\phi##. So we want the distribution of ##\phi## to be more concentrated around ##\phi=90^\circ## and less concentrated around ##\phi = 0^\circ,180^\circ##.

I don't know why, but intuitively I'm thinking perhaps we distribute ##\phi## like a circle, so that ##\phi = 90^\circ - \sqrt{90^2-x^2}## if ##x \in [0^\circ,90^\circ]## and ##\phi = 90^\circ - \sqrt{90^2-(x-180)^2}## if ##x \in [90^\circ,180^\circ]## where ##x## is the random number normalized from 0 to ##2\pi##.
First, let's look at the general situation where we have a random variable ##x## with pdf ##p(x)## mapped onto a second r.v. by ##y = g(x)##. What is the pdf for ##y##? Call it ##f(y)##?

First, the probability that ##y## lies between ##y## and ##y + dy## is ##f(y)dy##.

This is the same as the probability that ##x## lies between ##x + dx##, where ##y = g(x)## and ##y + dy = g(x + dx)##.

Using a Taylor expansion to linear order we see that:
$$dy = g(x + dx) - g(x) = g'(x)dx$$Which is just the usual "differential" equation.

This gives us$$f(y)g'(x)dx = p(x)dx$$Hence $$f(y) = f(g(x)) = \frac{p(x)}{g'(x)}$$To see how this works, for the circle we need probability of a point lying inside radius ##r## to be proportional to the area of that smaller circle. As area is proportional to ##r^2##, we need:
$$p(r \le a) \sim a^2$$And, as the cdf is the integral of the pdf, this means that the pdf for ##r## must be linear. For a circle of radius ##R## this gives:
$$f(r) = \frac{2r}{R^2}$$Now, if we have ##x## uniformly distributed on ##[0,1]##, then ##p(x) = 1## and we have an equation for ##r = g(x)##:
$$f(r) = f(g(x)) = \frac{2g(x)}{R^2} = \frac{1}{g'(x)}$$This gives us:
$$g(x)g'(x) = \frac{R^2}{2}$$Hence:
$$\frac d {dx} \big (g(x)^2 \big ) = 2g(x)g'(x) = R^2$$And this gives us the required mapping from ##x## to ##r##:$$r = g(x) = R\sqrt x$$We can also confirm this by calculating the probability of choosing a point in a more general area of the circle, between ##r_1## and ##r_2## and ##\varphi_1## and ##\varphi_2##:
$$A = \int_{r_1}^{r_2} \int_{\varphi_1}^{\varphi_2} r \ d\varphi \ dr = \frac 1 2 (r_2^2 - r_1^2)(\varphi_2 - \varphi_1)$$Now, we use the mappings from ##x,y## which are uniform on ##[0,1]##:
$$\varphi = 2\pi y, \ r = R\sqrt x \ \ \text{hence} \ \ y = \frac{\varphi}{2\pi}, \ x = \frac{r^2}{R^2}$$And:
$$p(A) = p(\frac{\varphi_1}{2\pi} \le y \le \frac{\varphi_2}{2\pi})p(\frac{r_1^2}{R^2} \le x \le \frac{r_2^2}{R^2}) = \frac{1}{2\pi R^2}(\varphi_2 - \varphi_1)(r_2^2 - r_1^2) = \frac{A}{\pi R^2}$$So, the probability of choosing a point in such an area is indeed proportional to the area.

For the sphere, we can have simply ##\varphi = 2\pi y##, but we need to map ##x## and ##z## to ##r## and ##\theta## taking account of the factors in the spherical volume element.