dimanche 23 octobre 2022

How to generate random numbers in pre-defined range which sums up to a fixed number in R/Python

I have a simple data generation question. I would request for any kind of help with the code in R or Python. I am pasting the table first.

Total Num1_betw_1_to_4 Num2_betw_1_to_3 Num3_betw_1_to_3
9 3 3 3
7 1 3 3
9 4 3 2
9 3 3 3
5 2 2 1
7 3 2 2
9 3 3 3
7 2 3 2
5
6
2
4
9

In the above table, first column values are given. Now I want to generate 3 values in column 2, 3 and 4 which sum up to value in column 1 for each row. But each of the column 2, 3 and 4 have some predefined data ranges like: column 2 value must lie between 1 and 4, column 3 value must lie between 1 and 3, and, column 4 value must lie between 1 and 3.

I have printed first 8 rows for your understanding. In real case, only "Total" column values will be given and remaining 3 columns will be blank for which values have to be generated.

Any help would be appreciated with the code.




Aucun commentaire:

Enregistrer un commentaire