Reputation: 1477
I want to create a random n x n matrix with decimal numbers as elements. Each element has to be greater than zero and each row/column should add up to one. The elements should otherwise be random except for the two restrictions described above.
So far, I have a lot of nested for loops using random.random()
but the code is really messy. Is there an easier way to do this? Thanks.
Upvotes: 0
Views: 542
Reputation: 304175
This is pretty easy if the elements are all allowed to be the same. Assuming they must all be distinct, you'll have a variation of a magic square
There are different algorithms to make a magic square depending on whether n
is odd or even.
Once you have a magic square, each row and column will sum to some constant N
Divide all the numbers in the square by N
Upvotes: 1