dimanche 10 septembre 2023

python random.choices() not giving equal number of elements even if we give equal weights

I want to create a random list with diffrent combinations of elements in the list. The count of each element appearing in the final list should same.

For example

>>> set_sizes = [0, 3, 6, 12]
>>> target_list = random.choices(set_sizes, weights = [1, 1, 1, 1], k= 40)
>>> print (target_list.count(0), target_list.count(3), target_list.count(6), target_list.count(12))

8 10 12 10

Here I was expecting the count of all elements will be 10 since I give equal weights. But it is not same always. How can I modify the code to get the equal count for all elements ? Please help.




Aucun commentaire:

Enregistrer un commentaire