jeudi 21 février 2019

get a list of unique items from Random.choices function

I have a method that is using the random package to generate a list with certain probability for example:

import random

seed = 30
rand = random.Random(seed)
options_list = [1, 2, 3, 4, 5, 6]
prob_weights = [0.1, 0.2, 0.1, 0.05, 0.02, 0.06]
result = rand.choices(option_list, prob_weights, k=4) # k will be <= len(option_list)

my problem is that result can hold two of the same item, and I want it to be unique.

I could make the k param much larger and then filter out the unique items but that seems like the wrong way to do that. I looked in the docs and I dont see that the choices function gets this kind of parameter.

Any ideas how to config random to return a list of unique items?




Aucun commentaire:

Enregistrer un commentaire