*(This post continues where we left off in Part 2 and gives a more direct solution than Part 3a. However this post uses multivariable/vector calculus, and I do not provide many explanations for readers unfamiliar with that material.)*

As we saw in Part 2, the problem with picking our spherical angles and uniformly was that the area on our two-dimensional grid changed when we transformed that surface into the sphere in three dimensions. In particular if we wanted to calculate the probability mass in a region of our spherical angles, we have the formula from Part 0:

where is our probability density function. However when we move to the sphere our area elements are no longer little rectangles with area ; they are little patches on the surface of a sphere. What area do these corresponding patches have?

**Figure 1:** The spherical coordinate system

In Part 1 we defined spherical coordinates graphically using the scheme shown in Figure 1. With some basic trigonometry we can describe the transformation from spherical to Euclidean coordinates by:

And so we can parameterize the surface of the unit sphere as a vector valued function

Now we consider our two-dimensional space of spherical angles and that we can think of as a rectangle where the coordinates of each point specify the longitude and co-latitude. Take any region R in this space. The formula for surface integration gives us that

where is the area of the region R after it has been transformed by the function onto the surface of the sphere in three-dimensional space, represents the area of the area of a little patch on the surface of the sphere and denotes the vector cross product.

^{[1]}

We wonâ€™t perform all the calculations here, but you can differentiate and use the rule to get that

since .

Therefore we get that the small patches on the surface of the sphere at a given longitude and co-latitude have area . This fits with what we saw in Part 2. At the equator () we have that the area element is . That means that at the equator, the area element on the surface of the sphere corresponds exactly with the area element on our equirectangular map. As we move away from the equator, we see that decreases and so the equally sized rectangles on our map will be transformed into increasingly smaller patches on the sphere. Once we reach the poles the area element vanishes.

Now that we have figured out exactly how area on our rectangular grid corresponds to area on the surface of the sphere, how do we solve the original problem? Recall from Part 0 that a probability density function defined on a sample space must:

- be non-negative at every point in
- integrate to 1 over .

Furthermore the uniform distribution on is such that the probability distribution is constant on . Therefore for a uniform distribution on the unit sphere , we must have the probability density function on the sphere where , a constant. The formula for integrating a function over a surface gives us that

We can see that the integral on the left gives the mass contained by the uniform distribution on the sphere over the region . The quantity on the right then must be the mass in the region on our map of and under the density .

^{[2]}Therefore if we could draw from this density function, we would be able to generate uniformly random points on the sphere.

Generating random samples from a given density is not a trivial problem. We will use a trick that will allow us to do this as long as we have a method to generate uniformly random samples on an interval (we do). If we go back to our surface integration formula, we can perform a change of variables to get

In other words, we see that drawing from our desired density is equivalent to drawing samples uniformly in and . Since takes all values in the interval for , we merely need to draw samples uniformly in the rectangle . This is exactly as we did in Part 3a where that rectangle was the Lambert cylindrical map.

While we have solved the original problem, this solution is cumbersome and extension to higher dimensions requires extra work. Continue on to Part 4 to see a different approach to the problem that extends well to any dimension and is computationally appealing.

Note that is equivalent to the determinant of the^{[1]}*Jacobian*of the spherical coordinates transformation evaluated at . I chose to show the solution to this problem directly using surface integrals, but the Jacobian is the standard way to handle changes of coordinates in integrals. ↩How do we know this is actually a density function? It is non-negative as for . It integrates to 1 over the entire region of spherical angles by the surface integral formula. ↩^{[2]}