>>85
Pythonで20面体のサイコロの全ての面が出るまで投げ続ける行為を1万回繰り返し、最も多い試行回数と出現数を表示するプログラムです。


import random
from collections import Counter

def roll_d20():
return random.randint(1, 20)

def main():
# 試行回数を繰り返す回数
num_trials = 10000
# 各試行の結果を保持するリスト
results = []

# 試行を繰り返す
for _ in range(num_trials):
# 全ての面が出揃うまでの試行回数
attempts = 0
# 出現した目のリスト
seen_numbers = []

# 全ての面が出揃うまで繰り返す
while len(seen_numbers) < 20:
# サイコロを投げる
result = roll_d20()
# 出現した目を記録
if result not in seen_numbers:
seen_numbers.append(result)
# 試行回数をカウント
attempts += 1

# 結果をリストに追加
results.append(attempts)

# 各試行回数の出現回数をカウント
counts = Counter(results)
# 最も多い試行回数とその出現回数を取得
most_common_attempt, most_common_count = counts.most_common(1)[0]

# 結果を出力
print(f"最も多い試行回数: {most_common_attempt} (出現回数: {most_common_count})")

if __name__ == "__main__":
main()