How do I sample a random rotation matrix in $n$ dimensions?
Let $M$ be a $n\times n$-dimensional matrix. $M$ is a rotation matrix if $\|Mx\|_2 = \|x\|_2$ for all $x \in \mathbb{R}^n$. I want to sample uniformly at random from the rotations, in the following sense. Let $\mathcal{S}=\{x: \|x\|_2=1\}$ denote the perimeter of a sphere of radius $1.$ I want a distribution on $M$ so that, if you fix any point $x \in \mathcal{S}$, then $Mx$ will be uniformly distributed on $\mathcal{S}$.
How can I sample a matrix $M$ from this distribution? I am looking for an algorithmic procedure I can use to sample such a matrix randomly.
I believe this corresponds to sampling uniformly at random from $O(n)$ (the orthogonal group). I am fine with either a procedure for sampling uniformly at random from $O(n)$ or $SO(n)$; it should be easy to convert between the two of them.