From Creating a matrix of arbitrary size where rows sum to 1?, we can simply normalize the values by dividing each value in the matrix by the sum of all values to make sure that it sums to 1.
For example:
cols, rows = 5, 5
matrix = np.random.rand(rows, cols)
matrix/matrix.sum(axis=1)[:,None]
How to create a matrix of arbitrary size where rows sum to 0?
I've tried:
cols, rows = 5, 5
x = np.random.rand(rows, cols)
z = x - sum(sum(x)) / (rows*cols)
It goes to a number close to zero but never summing zero =(