Expression for a Rotation Matrix
The matrix \(R = R(x,y,z,\theta)\) that rotates by angle \(\theta\) about the unit vector \(V=(x,y,z)\) is
$$R=
\begin{pmatrix}
x^2(1-\cos\theta) + \cos\theta &
x y(1-\cos\theta) – z \sin\theta &
z x (1-\cos\theta) + y \sin\theta\\
x y (1-\cos\theta) + z \sin\theta&
y^2 (1-\cos\theta) + \cos\theta&
y z (1-\cos\theta) – x \sin\theta\\
z x (1-\cos\theta) – y \sin\theta&
y z (1-\cos\theta) + x \sin\theta&
z^2 (1-\cos\theta) + \cos\theta
\end{pmatrix}$$
This is known as the Rodrigues rotation formula.
The trace (sum of diagonal elements) of this matrix is \(S = 1 + 2\cos\theta\). Using this property, given two rotation matrices \(M_1, M_2\) that preserve lengths, one can find the angle between them. Since
$$R M_1 = M_2 \Leftrightarrow R = M_2 M_1^{-1},$$
if \(S\) denotes the trace of \(M_2 M_1^{-1}\), the angle \(\theta\) between the two rotation matrices is
$$\theta = \arccos \frac{S-1}{2}.$$
Uniformly Distributed Random Vectors on a Sphere
Using uniform random numbers \(\alpha, \beta \in [0,1]\), a vector \(V\) distributed uniformly on the unit sphere is given by
$$V=\begin{pmatrix}
2 \beta -1 \\
2 \sin (2\pi\alpha) \sqrt{\beta(1-\beta)} \\
2 \cos (2\pi\alpha) \sqrt{\beta(1-\beta)}
\end{pmatrix}.$$
Random Rotation Matrix
Using uniform random numbers \(\alpha, \beta, \gamma \in [0,1]\), a uniformly distributed random rotation matrix \(M\) can be expressed as
$$M=\begin{pmatrix}
\cos(2\pi\alpha) & \sin(2\pi\alpha) & 0\\
-\sin(2\pi\alpha) & \cos(2\pi\alpha)& 0\\
0&0&1
\end{pmatrix}
\begin{pmatrix}
1&0&0\\
0&2\beta-1&2\sqrt{\beta(1-\beta)}\\
0&-2\sqrt{\beta(1-\beta)}&2\beta-1
\end{pmatrix}
\begin{pmatrix}
\cos(2\pi\gamma)&\sin(2\pi\gamma)&0\\
-\sin(2\pi\gamma)&\cos(2\pi\gamma)&0\\
0&0&1
\end{pmatrix}.$$