# Generate random points on a section of a spherical surface

If I have a sphere with radius 1, and a section of the sphere defined by having a polar angle bounded by some constant θ and an azimuthal angle bounded by some constant φ, how do I generate random points on this surface such that the points are uniformly distributed?

Consider the sphere patch defined as the part of the sphere whose azimuthal angle $$\theta$$ and polar angle $$\phi$$ satisfy $$0 \leqslant \theta_1 \leqslant \theta \leqslant \theta_2 \leqslant 2\pi$$ and $$0 \leqslant \phi_1 \leqslant \phi \leqslant \phi_2 \leqslant \pi$$ for given $$\theta_1$$, $$\theta_2$$, $$\phi_1$$, $$\phi_2$$.
Here is how to uniformly sample on this sphere patch. Take two independent random variables $$U_1 \sim \mathcal{U}(0,1)$$ and $$U_2 \sim \mathcal{U}(0,1)$$. Define $$\Theta = \theta_1(1-U_1) + \theta_2 U_1$$, define $$A = \cos(\phi_1)(1-U_2) + \cos(\phi_2)U_2$$, define $$B = \sin\bigl(\arccos(A)\bigr)$$. Then, denoting by $$r$$ the radius of the sphere, the random point $$r\bigl(\cos(\Theta)B, \sin(\Theta)B, A\bigr)$$ has the uniform distribution on the sphere patch.
Pick $$\varphi$$ uniformly randomly from the admissible interval, and pick $$z=\cos\theta$$ uniformly randomly from the admissible interval. This works because the sphere has equal areas in slices of equal height. (This is only the case in $$3$$ dimensions.)