You are selecting a random subset of the columns, and then for each column, a random row. Here's one way to do that using numpy. The binomial distribution is used to select which columns get a 1. Change the second argument of numpy.random.binomial
to tweak the density of columns with a 1.
In [156]: m = 5
In [157]: n = 12
In [158]: a = np.zeros((m, n), dtype=int)
In [159]: cols = np.random.binomial(1, 0.7, size=n)
In [160]: a[np.random.randint(0, m, size=cols.sum()), np.nonzero(cols)[0]] = 1
In [161]: a
Out[161]:
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0],
[1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0]])
If you want a 1 in each column, here's a fairly concise method:
In [103]: m = 5
In [104]: n = 12
In [105]: a = (np.random.randint(0, m, size=n) == np.arange(m).reshape(-1, 1)).astype(int)
In [106]: a
Out[106]:
array([[0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1],
[0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
np.random.randint(0, m, size=n)
is the random selection of row indices, one for each column. np.arange(m).reshape(-1, 1)
is the sequence [0, 1, ..., m-1]
stored in an array with shape (m, 1). When this is compared to the random values, broadcasting applies, so an boolean array with shape (m, n) is created. Just convert that to integers, and you have your result.