# Random Integer Sequences

I want to construct a random square matrix in the following manner: a) Three states [0 | 1 | 2] are repeated [2 | 4 | 6 | 8 | 10] times in each row

b) All numbers of current row must be different from the numbers directly above them

I have written:

Make one row

mrow := Module[{x},
Take[
Flatten @ Table[x = RandomInteger[{0, 2}];
Table[x, {RandomChoice[{2, 4, 6, 8}]}], {5}],
10]]


Prepare matrix with indexed variable row. Determine by subtraction whether next row is different. If not While - loop until a different row is found.

For[n = 2; row = mrow, n <= 10, n++,
row[n] = mrow;
While[MemberQ[row[n - 1] - row[n], 0], row[n] = mrow]];


Output matrix

Map[row[#] &, Range] // MatrixPlot Unfortunately, for the first time, I had to use For and While with MMA programming.

Do you see any functional way to get the desired result?

• BTW, there are 5 3^5 2^20 possible configurations. Not all of them are equally probable, though – Dr. belisarius Sep 23 '14 at 19:16

Here's an approach without If or For. First a helper function:

(* Thanks to Belisarius for the mrow& suggestion *)
g[x_] := NestWhile[mrow&, x, MemberQ[x - #, 0] &]


Then:

NestList[g, mrow, 9] // MatrixPlot


Where mrow is as you've defined it in the question. • +1. You don't need f[] ... g[x_] := NestWhile[mrow &, x, MemberQ[x - #, 0] &] – Dr. belisarius Sep 23 '14 at 19:19
• @Belisarius, Thanks, I knew my brain was failing me :). I'll update. – RunnyKine Sep 23 '14 at 19:23
• BTW I find this solution really nice – Dr. belisarius Sep 23 '14 at 19:27
• @Belisarius. Thanks. I almost gave up on it. – RunnyKine Sep 23 '14 at 19:42
• First time that I see a non-pure function (mrow) ending with a &. Some day I'll understand. Let me upvote in-between :) – eldo Sep 23 '14 at 21:11

Nice question. I'd try to do something with recursion instead, like

Clear[f];

f[n_] := f[n] = newRow[f[n - 1]]
f = ConstantArray[10, 10];

newRow[previousRow_] := With[{row = mrow},
If[MemberQ[previousRow - row, 0], newRow[previousRow], row]
]
Array[f, 10] // MatrixPlot

• Your elegant solution functions perfectly. Many thanks :) – eldo Sep 23 '14 at 16:58
 row := Take[
NestWhile[Join[#, ConstantArray[RandomInteger[{0, 2}],
RandomChoice[{2, 4, 6, 8}]]]  &, {} , Length@# < 10 &], 10]

Nest[Module[{b}, (While[Times @@ (#[[-1]] - (b = row)) == 0];
Append[#, b])] & , {row} , 9] // MatrixPlot