# Generating a list of outputs using For[] Loop

I have a question regarding the use of the For[] loop to generate a list of outputs from a function that generates random outputs. Essentially, creating a sample of outcomes.

The function F uses

Table[RandomChoice[{1,2,3,4},m}],{j,1,m}]


to basically create a m by m matrix whose components are 1,2,3, or 4 at random. I wish to come up with a For loop function that gives me a desired number of outcomes, say n, from this function, with m bound within a range of 1 to 10. How should I construct this For loop?

• Can you elaborate on why you insist on using For instead of Table? If you have not yet seen it, please read mathematica.stackexchange.com/q/134609/12 Commented Oct 24, 2020 at 18:31
• If you want n such random m by m matrices, you can use RandomChoice[{1, 2, 3, 4}, {n, m, m}], which is much simpler. Commented Oct 24, 2020 at 18:33

RandomInteger[{1, 4}, {5, 5}]


this generates a 5 by 5 array of random integers in the range {1,4}

Table[RandomInteger[{1, 4}, {5, 5}], {3}]


this generates 3 arrays

Table[RandomInteger[{1, 4}, {m, m}], {m, 4, 6}, {3}]


this generates 3 of each size starting at the 4 by 4 ending on the 6 by 6

Table[RandomInteger[{1, 4}, {m, m}], {m, 4, 6}]


this generates 1 array of each size

MapThread[Plus, Table[RandomInteger[{1, 4}, {5, 5}], {10}], 1]


this generates 10 random 5 by 5 arrays then adds the individual values in each array across all arrays to return a new 5 by 5 array that represents all the totals at each individual location. normally this is bad because every time we call it, the random numbers are different. this makes the original random data disappear immediately after the result of Plus is returned. to fix this we store the result in a variable a

a = Table[RandomInteger[{1, 4}, {5, 5}], {10}]


you can surpress the output with semicolon ;

a = Table[RandomInteger[{1, 4}, {5, 5}], {10}];


then later if you want to do something with a you have it there. you can make lists of lists

b = Join[b,a]


you can format the matrix for display purposes

a = Table[RandomInteger[{1, 4}, {5, 5}], {10}]
MatrixForm[myarraytotal]


Note: this is for display purposes. you should not store the result if you expect to do more computation later.

a = Table[RandomInteger[{1, 4}, {5, 5}], {10}];

• I think you should avoid MatrixForm here. Commented Oct 24, 2020 at 18:36
• I just thought there should be a warning against using MatrixForm in this way, if one wants to actually compute with the matrices that are generated. The same advice would go for Framed[Grid[]]. It's explained in the linked Q&A. Commented Oct 24, 2020 at 20:01
• My typical way would be to save your table in list = Table[...] and display the list with MatrixForm /@ list. In the Q&A, the OP wraps the matrix cov in MatrixForm and cannot compute with it. The answers explain the purpose of MatrixForm and how to work with it. Questions linked to that Q&A show it's a common problem: (126094), (166709), (218801), (225279)...60+ more... Commented Oct 24, 2020 at 20:38