Sum of random numbers to certain value for each row in Matlab?
3 views (last 30 days)
I would like to generate random numbers (non-decimal numbers) between range 3 and 5 (numbers can repeat within this range) to add up to a certain value lets say 48. I tried the following coding and it is working fine. I have a total of 14 values (columns) for one single entry (row).
The above data value of 48 is for a single entry. I have around a total of 39 entries each having its own summation value whereas the other constraints are same.
How can I define summation for each entry (row) and then get the summation of the random numbers of each row equal to that value? In the current coding if I increase the number of rows then summation does not work.
If possible, How may I let MATLAB randomly choose the summation values for all 39 entries between 40 and 70 and then distribute the sum of each row (entry) to that particular corresponding sum value?
Walter Roberson on 9 Mar 2022
Assign all 3 to each column.
For each row, if you have not reached the total for the row yet, determine the columns of the row which are 3 or 4. Pick one of them at random. Increment that column.