Random number generation in $3D$ I have problem regarding random number generation. Suppose I have disc of radius $r$.
$$
\begin{align}
x&=r\cos(\theta)\\
y&=r\sin(\theta)\\
z&=0
\end{align}
$$
I rotate the co-ordinate. So new co-ordinate is
$$
\begin{align}
X&=x\cos(\phi)+z\sin(\phi)\\
Y&=y\\
Z&=-x\sin(\phi)+ z\cos(\phi)
\end{align}
$$ 
I want to generate $r$, $\theta$ and $\phi$ by random number such that I get equal probability in all space. I used
$$
\begin{align}
r&=r_{\text{max}}\sqrt{\mathrm{random}(N)}\\
\theta&=2\pi\,\mathrm{random}(N)\\
\phi&=\phi_{\text{min}}+\mathrm{random}(N)*(\phi_{\text{max}}-\phi_{\text{min}})
\end{align}
$$
But it does not work I think. Can anybody help me?
 A: Suppose a random variable $X$ has the Cumulative Distribution Function
$\Phi(t)=P(X\le t)$:
$\hspace{3.2cm}$
The probability of $X$ being within the base of the green region, is the height of the red region. Thus, $\Phi(X)$ is uniformly distributed along the vertical axis, $[0,1]$. Therefore, $\Phi^{-1}(U)$ has the same distribution as $X$ where $U$ is uniformly distributed in $[0,1]$.
As long as the region is radial, the CDF of being within $t$ of the origin is
$$
\Phi(t)=\frac{t^3}{r^3}
$$
Thus, we get the proper distribution of the distance from the origin with
$$
r\,u^{1/3}\tag{1}
$$
where $u$ is uniform in $[0,1]$.
Below, we look at shapes that are radially symmetric, and we need to use the signed radius
$$
r\,(2u-1)^{1/3}\tag{2}
$$
As shown in this answer, the the Lambert cylindrical projection preserves area. Thus. to generate a uniformly distributed point on the surface of the sphere, we can choose $z$ to be uniformly distributed in whatever range we wish, and choose $x$ and $y$ uniformly around the (possibly partial) circle.

If the region is supposed to look like this between polar angles $\phi_{\text{min}}$ and $\phi_{\text{max}}$:
$\hspace{3.2cm}$
Then we would take
$$
\begin{align}
z&=\cos(\phi_{\text{max}})+(\cos(\phi_{\text{min}})-\cos(\phi_{\text{max}}))v\\
y&=\sin(2\pi w)\sqrt{1-z^2}\\
x&=\cos(2\pi w)\sqrt{1-z^2}
\end{align}\tag{3}
$$
where $v$ and $w$ are uniform in $[0,1]$.
To get the points in the sphere, multiply the points on the sphere from $(3)$ by the radius from $(2)$.

If the region is supposed to look like this between equatorial angles $\phi_{\text{min}}$ and $\phi_{\text{max}}$:
$\hspace{3.2cm}$
Then we would take
$$
\begin{align}
z&=2v-1\\
y&=\sin(\phi_{\text{min}}+(\phi_{\text{max}}-\phi_{\text{min}})w)\sqrt{1-z^2}\\
x&=\cos(\phi_{\text{min}}+(\phi_{\text{max}}-\phi_{\text{min}})w)\sqrt{1-z^2}
\end{align}\tag{4}
$$
where $v$ and $w$ are uniform in $[0,1]$.
To get the points in the sphere, multiply the points on the sphere from $(4)$ by the radius from $(2)$.

As verified by the author, the situation is supposed to be as generated by $(4)$. Using the coordinates given in the question, we get
$$
\begin{align}
r&=r_{\text{max}}(2u-1)^{1/3}\\
\theta&=\sin^{-1}(2v-1)\\
\phi&=\phi_{\text{min}}+(\phi_{\text{max}}-\phi_{\text{min}})w
\end{align}\tag{5}
$$
where $u,v,w$ are uniform in $[0,1]$. If you don't like negative $r$, when $r\lt0$, use
$$
\begin{align}
r'&=-r\\
\theta'&=\theta+\pi\\
\phi'&=\phi
\end{align}\tag{6}
$$

10000 Points: Generated using $(2)$ and $(3)$ (top is approaching; bottom is receding)
$\hspace{3.5cm}$

10000 Points: Generated using $(2)$ and $(4)$ (top is approaching; bottom is receding)
$\hspace{3.5cm}$
A: If you want your coordinates uniform over a sphere with radius $R$, you want the density of $r$ to be proportional to $r^2$.  Section 7.2 of Numerical Recipes describes how to transform uniform deviates on $[0,1]$ to a desired distribution when you can integrate the inverse function.  here you want $p(y)dy=r^2\ dy$, so need to find a function $y(x)$ such that $\left | \frac {dx}{dy} \right |=y^2$, which we can see to be $x=\frac 13y^3, y=\sqrt[3]{3x}$ so your $r$ coordinate is $R\sqrt[3]{x}$ for $x$ a standard random.  $\phi$ is easy-you want it uniform on $[0,2\pi]$ so just multiply a random by $2\pi$.  For $\theta$, you want the density proportional to $\sin \theta$, so you need $\left | \frac {dx}{dy} \right |=\sin y$, then $x=\cos y$ and your relation is $y=\arccos (2x-1)$
