Random Rotation Matrix in Python

Making a random rotation matrix is somewhat hard. You can’t just use “random elements”; that’s not a random matrix.

First attempt: Rotate around a random vector

My first thought was the following:

1. Pick a random axis $\hat u$, by getting three Gaussian-distributed numbers, calling them x, y, and z, and then taking the norm of that vector.
2. Pick a random angle in the range $% $.
3. Rotate your original vector $\vec v$ around $\hat u$ by $\theta$.

That’s accomplished with the following code:

Unfortunately, this does not give you a uniformly rotated vector: you end up where you started too often.

Second attempt: Uniform Random Rotation Matrix

If we want a uniformly distributed rotation, that’s a little trickier than the above. There’s a Wikipedia article section on it, but its not too helpful.

Instead, I found some C code from a book called “Graphics Gems III”.

Then, I started with a direct translation of the C code:

Then I did a more Pythonic version, using numpy arrays more to their potential, and adding an option to use pre-generated random numbers:

This method does a much better job of creating a uniform distribution. In other words, for any vector $\vec v$, after multiplying $\vec v^\prime = M \vec v$ (or v2 = M.dot(v)), the new vector $\vec v^\prime$ will be uniformly distributed over the sphere.