# How to generate random matrices with contraints?

I have a matrix

mat = {{Sqrt[(1 + a0 + a3)/2], (a1 - I a2)/Sqrt[2 (1 + a0 + a3)]}, {0, Sqrt[(1 + a0 - a3)/2 - (a1^2 + a2^2)/(2 (1 + a0 + a3))]}};

How can I randomly generate mat by choosing a0 a1 a2 and a4 randomly subjeceted to condition a0 + a1^2 + a2^2 + a3^2 <= 1?

• It all depends on what distribution you want to draw the random numbers from. Could you please be more explicit on this point? Commented Jul 26, 2021 at 17:22

Clear["Global*"]

(mat = {{Sqrt[(1 + a0 + a3)/2], (a1 - I a2)/Sqrt[2 (1 + a0 + a3)]}, {0,
Sqrt[(1 + a0 - a3)/2 - (a1^2 + a2^2)/(2 (1 + a0 + a3))]}});


Assuming that -1 <= a0, a1, a2, a3 <= 1

rgn = ImplicitRegion[
a0 + a1^2 + a2^2 + a3^2 <= 1 &&
And @@ Thread[-1 <= {a0, a1, a2, a3} <= 1], {a0, a1, a2, a3}];

SeedRandom[1234];

mat /. Thread[{a0, a1, a2, a3} -> RandomPoint[rgn]]

(* {{0. + 0.0949935 I, -0.460629 - 4.49785 I}, {0, 4.52495}} *)

• Thanks, Bob. I was expecting different matrix elements every time I run the code. Commented Jul 26, 2021 at 19:09
• Each time you execute the last line mat /. Thread[{a0, a1, a2, a3} -> RandomPoint[rgn]] by itself, you will get a different result. If you are also executing SeedRandom[1234];` it reinitializes from the same starting point and repeats. Commented Jul 26, 2021 at 19:15
• Got it. Thanks a lot! Commented Jul 26, 2021 at 19:17
• Is it possible to do this process as: Let a1^2+a2^2+a3^2=a^2. Then start with a0=0, a=1 all the way to a0=1, a=0 and find all matrices in this range of parameters? Should I ask this as a separate question? Commented Jul 26, 2021 at 19:22
• Yes, ask a new question and explain in detail what you want. Commented Jul 26, 2021 at 21:54