2016-05-10 8 views
0

長いリストが2つあります。 例:%の確率でリストから一意の結果を返します

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

選手の数は変更することができます。

を各プレイヤーは良いことが起こることを43%の確率で(あるいは悪いことが起こることを57%の確率で)持っています。 2人の選手が同じ成果を出すことはできません。

私はこのような重み付けされた結果実行しようとしました:

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%で、まだすべてのプレーヤーは良いものを持っていた可能性があるはずですので、私は、感じ

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

:私は70%の良好な結果と30%の悪い結果を持って、この例のようにすべてのプレイヤーが何か悪いことが起こる可能性が低いということです。

答えて

1

なぜifを使用できないのですか?ここにあなたのプレーヤーのためのユニークなthingsを持っている例

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私はリストを一時的なリストにコピーしています。

+0

これは、私が各プレーヤーに一意の値を持たせるためには機能しません。 – Chad

+0

@Chadこの部分が欠落しているに違いありません: "そして、この良い/悪いものをリストから削除してください。" –

+0

@Chad私はより完璧な解決策を示すために最初のバージョンを編集しました。 – Laszlowaty

関連する問題