mardi 21 septembre 2021

Generate random numbers with two constraints (sum and local maximum) in Python

I have a dataframe where I want to create random numbers in a new column. The random numbers must fulfill two constraints:

  1. The random numbers must add up to a specified sum (in the example, the sum is 300)
  2. For each observation, the random numbers must not exceed a value in the constraint column.

In the example below, the constraints are fulfilled because the sum is 300 and the random number does not exceed the constraint column.

Example:

GEOID CONSTRAINT RANDOM
010010000001 100 80
010010000002 50 40
010010000003 75 60
010010000004 75 60
010010000005 100 60

It seems having random numbers totaling a sum has been demonstrated but I do not see an example with a second constraint.




Aucun commentaire:

Enregistrer un commentaire