# Randomly pick ten numbers from a table such that no two numbers are on the same row or the same column

I have generated a matrix as follows:

   Range[41, 50], Range[51, 60], Range[61, 70], Range[71, 80],
Range[81, 90], Range[91, 100]]) // MatrixForm


I was wondering how I could select ten items from this matrix so that no two numbers are from the same row or the same column. Thank you.

• Your matrix has only 6 rows. Therefore, it is not possible to choose 10 numbers that have no row in common. On the other hand, if you have a 10x10 matrix, the 10 diagonal elements have no row or column in common. Oct 7, 2022 at 20:35

theMatrix = ArrayReshape[Range[100], {10, 10}]


Choosing any item from each row satisfies the unique row constraint. Satisfying the column constraint means that, as we choose items from each row, we must use each index in the range 1..n exactly once. So, we can create the column indices like this:

theIndices = RandomSample[Range@10, 10]


Now, we just need to take the items theMatrix[[1,theIndices[[1]]]], theMatrix[[2,theIndices[[2]]], etc. One straightforward way to do that would be with MapThread:

MapThread[Take, {theMatrix, List /@ theIndices}]


You could also build up the 2-d indices first and use Extract:

thePositions = Transpose[{Range@10, RandomSample[Range@10, 10]}];
Extract[theMatrix, thePositions]


I don't know what format you want your result in.

NOTE

user293787's answer reminded me that you don't need to specify the second argument in RandomSample if you want a random permutation of the list. So, all of my RandomSample[Range@10, 10] above could be replaced with RandomSample[Range@10].

Without constructing the matrix first, one could use:

10*Range[0,9]+RandomSample[Range[10]]

SeedRandom[1];
arr = RandomChoice[Alphabet[], {10, 10}];

g1 = arr // Grid


$$\begin{array}{cccccccccc} \text{f} & \text{a} & \text{h} & \text{a} & \text{c} & \text{d} & \text{a} & \text{v} & \text{a} & \text{q} \\ \text{x} & \text{o} & \text{d} & \text{i} & \text{t} & \text{f} & \text{s} & \text{q} & \text{m} & \text{a} \\ \text{t} & \text{e} & \text{w} & \text{v} & \text{h} & \text{d} & \text{a} & \text{e} & \text{u} & \text{y} \\ \text{d} & \text{f} & \text{m} & \text{t} & \text{v} & \text{i} & \text{v} & \text{y} & \text{l} & \text{z} \\ \text{z} & \text{c} & \text{d} & \text{k} & \text{e} & \text{c} & \text{x} & \text{r} & \text{l} & \text{p} \\ \text{g} & \text{m} & \text{k} & \text{c} & \text{w} & \text{k} & \text{e} & \text{s} & \text{m} & \text{o} \\ \text{z} & \text{m} & \text{e} & \text{l} & \text{m} & \text{c} & \text{f} & \text{u} & \text{g} & \text{k} \\ \text{y} & \text{f} & \text{h} & \text{s} & \text{f} & \text{c} & \text{k} & \text{o} & \text{z} & \text{v} \\ \text{e} & \text{p} & \text{r} & \text{e} & \text{w} & \text{i} & \text{g} & \text{y} & \text{d} & \text{u} \\ \text{n} & \text{d} & \text{b} & \text{r} & \text{u} & \text{d} & \text{l} & \text{z} & \text{i} & \text{f} \\ \end{array}$$

First generate the indices using distinct numbers:

r1 = RandomSample[Range[10]]
r2 = RandomSample[Range[10]]
idx = Transpose[{r1, r2}]


{{3,9},{7,1},{9,4},{1,7},{5,8},{2,5},{6,2},{4,3},{10,6},{8,10}}

To visualize:

cdata = ColorData["Rainbow"][#/10] & /@ Range[10]

Extract[arr, idx]