Q:

Sparse Matrix One 1 for Every Row

I would like to generate a random matrix MxN where every rows has just a single one in a random position. 
For example, I would a matrix like this:
Out[3]: 
array([[1, 0, 0],
       [0, 1, 0],
       [0, 1, 0],
       [1, 0, 0],
       [0, 0, 1]])

I tried with
M = 5
N = 3
arr = np.array([1]   + [0] * (N-1)) 
arr = np.tile(arr,(M,1))
np.random.shuffle(arr) 

But it gives:
Out[75]: 
array([[1, 0, 0],
       [1, 0, 0],
       [1, 0, 0],
       [1, 0, 0],
       [1, 0, 0]])

A:

There may be a more elegant way to do this, but works:
def randOne():
    M = 5
    N = 3
    arr = np.zeros((M, N))
    for row in range(M):
        arr[row, np.random.randint(N)] = 1
    return arr

>>> randOne() array([[ 0.,  0.,  1.],
       [ 1.,  0.,  0.],
       [ 0.,  0.,  1.],
       [ 0.,  1.,  0.],
       [ 1.,  0.,  0.]])

OR
Yup, there is a more elegant way to do this ;)
def randOne2(M=5, N=3): 
    arr = np.zeros((M, N), dtype=np.int8)
    arr[np.arange(M),np.random.randint(0,N,M)] = 1
    return arr

>>> randOne2()
array([[0, 0, 1],
       [1, 0, 0],
       [1, 0, 0],
       [0, 1, 0],
       [1, 0, 0]], dtype=int8)

