# Random orthogonal matrix

Is there a way to generate real random orthogonal matrices in mathematica? If Q is an orthogonal matrix, it's properties are

• Have a look at Orthogonalize. Apr 9, 2021 at 8:09

RandomVariate[CircularRealMatrixDistribution[n]] should do the trick for $$n \times n$$ matrices. Very obscure name IMHO.

• Can I fix the random matrix once produced Apr 9, 2021 at 9:18
• Not sure what you mean. Of course, you can store the result in a variable: A = RandomVariate[CircularRealMatrixDistribution[n]] and use A from then on. If you want to have reproducible pseudorandom matrices, you can use SeedRandom to set a seed for the random number generator. Apr 9, 2021 at 9:22
• What I meant is seedrandom. Do I just add seed before random Apr 9, 2021 at 9:38
• "Do I just add seed before random " Yes. See also the documentation. Apr 9, 2021 at 9:39
• I would like to know if there is any way to rescale all the elements of the resulting matrix so that they like in a given interval, say $[-r,r]$ as specified by the user. I realised that the CircularRealMatrixDistribution ensures that the elements are in the interval $[-1,1]$ but I wanted the same for a general interval. Nov 17, 2022 at 9:37

Here is a pedestrian way of getting a real orthogonal random matrix:

The eigenvalues of an orthogonal matrix are +1 or -1. Therefore, we may create a diagonal matrix with +1 or -1 on the diagonal and the rotate this matrix by a random rotation:

n = 3;
mat0 = DiagonalMatrix[RandomChoice[{-1, 1}, n]];
rot = RotationMatrix[RandomReal[{-1, 1}, {2, n}]];
mat1 = Transpose[rot].mat0.rot;
OrthogonalMatrixQ[mat1]


(* True *)

• Are you sure you're sampling from a uniform distribution in orthogonal-matrix-space here? Apr 9, 2021 at 9:11
• What is the objection? Apr 9, 2021 at 9:13
• My objection is that if I execute your recipe many times and plot the spatial distribution of the eigenvectors of the resulting matrices, I do not get a uniform distribution. Apr 9, 2021 at 9:17
• Interestingly that is true. Do you have an explanation? Apr 9, 2021 at 9:44
• (1) you're pulling the 3D coordinates from a cube instead of a sphere; should be RandomVariate[NormalDistribution[], {2, n}] instead of RandomReal[{-1, 1}, {2, n}]. (2) I don't think RotationMatrix does the right thing here. Apr 9, 2021 at 13:46