Return 基于百分比机会的列表中的唯一结果

Return a unique result from a list based on a percent chance

我有两个长列表。 示例:

good = ["Good thing1", "Good thing2", "Good thing3", "Good thing4", "Good thing5", ...]
bad = ["Bad thing1", "Bad thing2", "Bad thing3", "Bad thing4", "Bad thing5", ...]

可以更改的玩家人数:

players = 5

每个玩家都有 43% 的几率发生好事(或 57% 的几率发生坏事)。没有两个玩家会有相同的好或坏的结果。

我试着做这样的加权结果:

weighted_outcome = good * 43 + bad *57
random.sample(weighted_outcome, 5) # Where 5 is the number of players

但我得到了重复项。如果我将其作为一组来执行,那么加权结果将变为未加权,如本例所示:

weighted_outcome = good * 43 + bad *57
random.sample(list(set(weighted_outcome)), 5) # Where 5 is the number of players

我假设进行此加权的唯一方法是创建一个新的唯一加权列表是否正确?就像在这个例子中,我有 70% 的好结果和 30% 的坏结果:

weighted_result = []
weighted_result.append(random.sample(set(good), 7)
weighted_result.append(random.sample(set(bad), 3)
print(weighted_result)

我觉得这仍然是错误的,因为在 70% 时,每个玩家都有可能发生好事,而每个玩家都有坏事发生的可能性很小。

为什么不能使用if?这是一个例子

from random import randint
good_things = [good1, good2]
bad_things = [bad1, bad2]
players = [player1, player2]
temp_good_things = good_things[:]  # coping the lists
temp_bad_things = bad_things[:]
chance_for_good = 47
for player in players:
    if randint(0, 100) <= change_for_good:
        good_thing = get_random_element(temp_good_things)
        temp_good_things.remove(good_thing)
        player.do_thing(good_thing)
    elif:
        bad_thing= get_random_element(temp_bad_things)
        temp_bad_things.remove(bad_thing)
        player.do_thing(bad_thing)  

为您的玩家提供唯一 things 的关键是从列表中删除使用过的元素。为了保存基础 things 我正在将列表复制到临时列表中。