mardi 13 avril 2021

Generate random weights numbers with limited sum

I need to generate a random list of 8 integers between [0, 4] with weights and the sum should be 12.

Something like this:

from random import choices

while True:
    lst = choices(population=[0, 1, 2, 3, 4], weights=[0.20, 0.30, 0.30, 0.15, 0.05], k=8)
    if sum(lst) == 12:
        print(lst)
        break

There is a smarter way to do that?




Aucun commentaire:

Enregistrer un commentaire