# Variable Definitions and Random Numbers [closed]

I have currently created the following matrix:

m={{RandomReal[{0,1}],RandomReal[{0,1}]},{RandomReal[{0,1}],RandomReal[{0,1}]}}


In other words, a $2 \times 2$ matrix where the entries are real numbers. However, when I try to set $m$ to be a variable (to use it later to compute, say eigenvalues or eigenvectors), when I type m again, I obtain a different matrix than the one first obtained. I'm guessing that this is due to the fact that typing m will compute the matrix with other random reals. However, is there a way to store the matrix that is first obtained?

Say if m outputs the matrix {{1,2},{3,4}}, how can I fix this matrix for later use without obtaining a new one? Thank you for your help.

As long as you use Set (m =) rather than SetDelayed (m :=) the matrix will not be given new values unless you reevaluate the definition of m.

SeedRandom;

Clear[m]

m = RandomReal[{0, 1}, {2, 2}]


{{0.817389, 0.11142}, {0.789526, 0.187803}}

m


{{0.817389, 0.11142}, {0.789526, 0.187803}}

m


{{0.817389, 0.11142}, {0.789526, 0.187803}}

m has a fixed value. Compare with

SeedRandom;

Clear[m]

m := RandomReal[{0, 1}, {2, 2}]

m


{{0.817389, 0.11142}, {0.789526, 0.187803}}

m


{{0.241361, 0.0657388}, {0.542247, 0.231155}}

• Thank you! Are the lines of code SeedRandom and Clear[m] required? Or they are just for "cleaning" purposes? – hoyast Aug 9 '15 at 4:07
• They are not required. Using SeedRandom ensures that you can exactly repeat the same results if you reevaluate the notebook. This can help in troubleshooting with random numbers. – Bob Hanlon Aug 9 '15 at 4:18