エントリーの編集
エントリーの編集は全ユーザーに共通の機能です。
必ずガイドラインを一読の上ご利用ください。
記事へのコメント0件
- 注目コメント
- 新着コメント
このエントリーにコメントしてみましょう。
注目コメント算出アルゴリズムの一部にLINEヤフー株式会社の「建設的コメント順位付けモデルAPI」を使用しています
- バナー広告なし
- ミュート機能あり
- ダークモード搭載
関連記事
重み付け乱択 - Qiita
import random def random_weight_choice(L): choice = None total = 0 for item, p in L: total += p i... import random def random_weight_choice(L): choice = None total = 0 for item, p in L: total += p if random.random() * total < p: choice = item return choice def test_random_weight_choice(): from collections import defaultdict X = [('A', 3), ('B', 2), ('C', 5)] count = defaultdict(int) for _ in xrange(100000): item = random_weight_choice(X) count[item] += 1 print count if __name__ == '__main__': tes

