So the full iterative solution is:
In [158]: ll=[]
In [159]: for _ in range(10):
...: ll.append(np.random.choice(5,3))
In [160]: ll
Out[160]:
[array([3, 2, 4]),
array([1, 1, 3]),
array([0, 3, 1]),
...
array([0, 3, 0])]
In [161]: np.array(ll)
Out[161]:
array([[3, 2, 4],
[1, 1, 3],
...
[3, 0, 1],
[4, 4, 2],
[0, 3, 0]])
That could be cast as list comprehension: np.array([np.random.choice(5,3) for _ in range(10)])
.
Or an equivalent where you A=np.zeros((10,3),int)
and A[i,:]=np.random...
In other words you want choices from range(5)
, but want them to be unique only within rows.
The np.random.choice
docs suggest an alternative:
>>> np.random.choice(5, 3, replace=False)
array([3,1,0])
>>> #This is equivalent to np.random.permutation(np.arange(5))[:3]
I'm wondering if I can generate
array([[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
...
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4]])
and permute values within rows. But with permute
I can only shuffle all the columns together. So I'm still stuck with iterating on rows to produce the choice without replacement.