How do I write a python program that can randomly generate 4 columns of data such that the sum of the numbers of each row is 100?
How do I write a python program that can randomly generate 4 columns of data such that the sum of the numbers of each row is 100?
>>> import numpy as np
>>> A = np.random.rand(10,4)
>>> A /= A.sum(axis=1)[:,np.newaxis]
>>> A *= 100
>>> Aarray([[ 52.65020485, 8.39068184, 4.89730114, 34.06181217],[ 58.32667159, 8.99338257, 13.7326809 , 18.94726494],[ 8.23847677, 36.27990343, 14.73440883, 40.74721097],[ 37.10408209, 5.31467062, 39.47977538, 18.10147191],[ 21.5697797 , 14.80630725, 12.69891923, 50.92499382],[ 15.46006657, 24.62499701, 37.37736874, 22.53756768],[ 6.66777748, 25.62326117, 11.80042839, 55.90853296],[ 38.81602256, 26.74457165, 3.4365655 , 31.00284028],[ 5.67431732, 7.57571558, 44.01330459, 42.73666251],[ 33.09837171, 26.66421892, 10.90188895, 29.33552043]])
This generates positive real numbers as you asked. They will be random in the uniform distribution. If you want a different distribution, you can find several other choices in np.random
.