Randomly generate an matrix $A$ s.t. $A^m = I$ Fixed $n$, I want to randomly generate a $n \times n$ real matrix $A$ from the set:
$\{A \in \mathcal{M}_{n \times n}(\mathbb{R}): \exists m \in \mathbb{N} \mbox{ s.t. } A^m = I\}$
I think I should first randomly generate a diagonal matrix $D$ such that $\det(D) = \pm 1$ and then randomly generate an invertible matrix $P$ and then compute $PDP^{-1}$. Is this method correct?
Since $|\det(D)|=1$, I just generate a random $n$ unity ($+1$ or $-1$ for $\mathbb{R}$) to get the matrix $D$.
But how can I randomly generate an invertible matrix $P$?
To make the problem possible to solve, I should add a constraint like the matrix norm $\Vert P \Vert$ of P should satisfy: 
$0 < m \leq \Vert P \Vert \leq M$ for some constant $m,M$
 A: Here, this method leads to a probability invariant under the action of change of basis (Haaar measure).
This seems to be correct: 


*

*for the complex case:
you have to compute elements randomly chosen from the set$$
\{\exp iq | q\in \mathbb Q\cap[0,2\pi]
\}$$

*for the real case: simply take a matrix whose diagonal blocks take the form
$$\left[
\begin{matrix}
  0 & I_{m-1} \\
  1 & 0 \\
 \end{matrix}
\right]
$$ with $0$s elsewhere.
To generate $P$, the easiest way is to use the $LU$ decomposition.
$L$ and $U$ simulations are similar, and you can do the following:


*

*let $k=1$

*simulate $k$ random numbers, the last one being $\neq 0$: the is the $k^{\rm th}$ column of the matrix

*if $k<n$ make $k\to k+1$ and go back to 2.

A: I read the mookid's post ; unfortunately, its contains several incorrect details:


*

*The correct set is $\{e^{2i\pi q};q\in\mathbb{Q}\cap [0,1)\}$. Yet, what is the sense of "I randomly chose $q\in \mathbb{Q}\cap [0,1)$" ?  

*What is the sense of "I simulate $k$ random (real) numbers" ?


In both cases, we must chose a density on $\mathbb{Q}$ or $\mathbb{R}$.
Randomly chosing the real matrix $P=[p_{ij}]$ is not difficult, because we may replace $P$ with $aP$ where $a$ is real. Thus we may assume that  $|p_{i,j}|\leq 1$ and we use the uniform distribution of the entries on $[-1,1]$ (with $10$ digits for example). Of course, such a matrix $P$ is "always" invertible.
About the real matrix $D$ ($D$ is a block diagonal matrix where the blocks are $2\times 2$): firstly, we randomly chose a value of $m$ (with a density over $\mathbb{N}$). Secondly we randomly chose integers $p_r\in [-m,m]$ and we consider the blocks $\begin{pmatrix}\cos(2\pi  p_r/m)&-\sin(2\pi p_r/m)\\\sin(2\pi p_r/m)&\cos(2\pi p_r/m)\end{pmatrix}$. Of course, randomly, (if $m$ is a great number) we "never" obtain the eigenvalues $\pm 1$. Yet, if $n$ is odd, then we must chose the last eigenvalue in $\{\pm 1\}$.
