# Uniform sampling from part of sphere surface

I'd like to pose a question about uniform sampling on the surface of a sphere.

I searched this site, and uniform sampling on a sphere surface seems to be quite a common problem. The common solution is to take two uniform random numbers $u$ and $v$ between 0 and 1, then compute the spherical coordinates as follows:

$\theta = 2\pi u$ $\phi = \cos^{-1}(2v - 1)$

However, in my application I am required to sample a point uniformly on a small patch on the surface, namely with $\theta$ between $\theta_{min}$ and $\theta_{max}$ and with $\phi$ between $\phi_{min}$ and $\phi_{max}$. I could re-pick random numbers until these requirements are met, but since the patch can be quite small and efficiency is an issue (this is for a ray-tracer), I would like to know if there is an algebraic solution that involves limiting the range of the random numbers $u$ and $v$.

• If you can generate random numbers $u$ in $[0,1)$, then $a + (b - a)u$ is a random number in $[a,b)$. – fgp Mar 14 '14 at 0:59
• True, but how do I use this such that the resulting phi and theta are in the required ranges? I'm sorry if this was unclear. – Ghostkeeper Mar 14 '14 at 1:02
• You can solve for $u_{min}$, $u_{max}$, $v_{min}$, $v_{max}$, from the ranges you specified. For example, $2v_{min}-1 = \cos(\phi_{max})$. – Stephen Montgomery-Smith Mar 14 '14 at 1:06
• Just use to formula to pick suitable values for $\theta$ and $\phi$ from your desired range. Btw, why do you have that arc cos in your formula for $\phi$? – fgp Mar 14 '14 at 1:06
• @Ghostkeeper Yeah, I figured it out seconds after hitting "Add Comment". They're there because otherwise the sampling isn't uniform, since the length of the circle that all values with a fixed $\theta$ lie on depends on $\theta$. Sorry of the noise. – fgp Mar 14 '14 at 1:11

Based on the pointers given by Stephen Montgomery-Smith and fgp in the comments above, I came to the following formulas. I computed $u_{min}$ and $u_{max}$ from the inverse of the formula for $\theta$: $u = \frac{\theta}{2 \pi}$. I computed $v_{min}$ and $v_{max}$ from the inverse of the formula for $\phi$: $v = \frac{\cos(\phi) + 1}{2}$. Plugging $\theta_{min}$, $\theta_{max}$, $\phi_{min}$ and $\phi_{max}$ into their respective formulae, I obtained $u_{min}$, $u_{max}$, $v_{min}$ and $v_{max}$.
From there on, it's a matter of taking a uniform random number between $u_{min}$ and $u_{max}$ by taking $u = u_{min} + random() * (u_{max} - u_{min})$ (and for $v$ similar).